From a4cbb0429fab246498c160b42be977851640b694 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 20 Nov 2023 21:39:00 +0000 Subject: [PATCH] Parse target as an optional positional argument Instead of relying on parsing unknown args add a hidden placeholder positional argument so that target can be passed as a keyword arg, or as the only positional arg --- github_activity/cli.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/github_activity/cli.py b/github_activity/cli.py index 473268d..9ad46c8 100644 --- a/github_activity/cli.py +++ b/github_activity/cli.py @@ -118,18 +118,27 @@ help=("""Whether to include all the GitHub tags"""), ) +# Hidden argument so that target can be optionally passed as a positional argument +parser.add_argument( + "_target", + nargs="?", + default=None, + help=argparse.SUPPRESS, +) + def main(): if not _git_installed_check(): print("git is required to run github-activity", file=sys.stderr) sys.exit(1) - args, unknown = parser.parse_known_args() - # If we have unknown, it is the target - # TODO: this feels sub-optimal, we should be able to just treat positional args - # as optional. - if unknown and not args.target: - args.target = unknown[0] + args = parser.parse_args() + if args.target and args._target: + raise ValueError( + "target cannot be passed as both a positional and keyword argument" + ) + if not args.target: + args.target = args._target tags = args.tags.split(",") if args.tags is not None else args.tags # Automatically detect the target from remotes if we haven't had one passed.