-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bewest Implement optional default subcommand #1
Open
bewest
wants to merge
5
commits into
master
Choose a base branch
from
bewest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There's a test in [email protected]:1202975.git http://gist.github.com/1202975 * test_opt_subcommand.py * s.py That gives this error: bewest@ripen:~/Documents/git/gist/cli-hello$ python test_opt_subcommand.py -v test_optional_subcommand_behavior (__main__.TestSubparserDefaultFun) ... ['add subparsers', <class 'argparse._SubParsersAction'>, {'default': 'qux', 'dest': 'command', 'help': 'some help on group A', 'parser_class': <class 'argparse.ArgumentParser'>, 'prog': 'my program'}] ['consume optionals'] ['remaining positionals', [_SubParsersAction(option_strings=[], dest='command', nargs='A?..', const=None, default='qux', type=None, choices={'lux': ArgumentParser(prog='my program lux', usage=None, description='long desc of lux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'qux': ArgumentParser(prog='my program qux', usage=None, description='long desc of qux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'bux': ArgumentParser(prog='my program bux', usage=None, description='long desc of bux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'fux': ArgumentParser(prog='my program fux', usage=None, description='long desc of fux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, help='some help on group A', metavar=None)]] ([-AO]*) A?.. ['match arguments partial', '([-AO]*)', <_sre.SRE_Match object at 0x93f34a0>] ['consume positionals', {'arg_counts': [0], 'arg_strings': [], 'arg_strings_pattern': '', 'match_partial': <bound method ArgumentParser._match_arguments_partial of ArgumentParser(prog='test_opt_subcommand.py', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)>, 'positionals': [_SubParsersAction(option_strings=[], dest='command', nargs='A?..', const=None, default='qux', type=None, choices={'lux': ArgumentParser(prog='my program lux', usage=None, description='long desc of lux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'qux': ArgumentParser(prog='my program qux', usage=None, description='long desc of qux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'bux': ArgumentParser(prog='my program bux', usage=None, description='long desc of bux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'fux': ArgumentParser(prog='my program fux', usage=None, description='long desc of fux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, help='some help on group A', metavar=None)], 'selected_pattern': '', 'self': ArgumentParser(prog='test_opt_subcommand.py', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'start_index': 0, 'take_action': <function take_action at 0x95c1a04>}] ['loop', _SubParsersAction(option_strings=[], dest='command', nargs='A?..', const=None, default='qux', type=None, choices={'lux': ArgumentParser(prog='my program lux', usage=None, description='long desc of lux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'qux': ArgumentParser(prog='my program qux', usage=None, description='long desc of qux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'bux': ArgumentParser(prog='my program bux', usage=None, description='long desc of bux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'fux': ArgumentParser(prog='my program fux', usage=None, description='long desc of fux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, help='some help on group A', metavar=None), 0] ['get values', _SubParsersAction(option_strings=[], dest='command', nargs='A?..', const=None, default='qux', type=None, choices={'lux': ArgumentParser(prog='my program lux', usage=None, description='long desc of lux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'qux': ArgumentParser(prog='my program qux', usage=None, description='long desc of qux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'bux': ArgumentParser(prog='my program bux', usage=None, description='long desc of bux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'fux': ArgumentParser(prog='my program fux', usage=None, description='long desc of fux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, help='some help on group A', metavar=None), []] ['VALUE', 'qux'] ['take action', _SubParsersAction(option_strings=[], dest='command', nargs='A?..', const=None, default='qux', type=None, choices={'lux': ArgumentParser(prog='my program lux', usage=None, description='long desc of lux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'qux': ArgumentParser(prog='my program qux', usage=None, description='long desc of qux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'bux': ArgumentParser(prog='my program bux', usage=None, description='long desc of bux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'fux': ArgumentParser(prog='my program fux', usage=None, description='long desc of fux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, help='some help on group A', metavar=None), []] ['select parser', {'arg_strings': 'ux', 'namespace': Namespace(command='q', debug=False), 'option_string': None, 'parser': ArgumentParser(prog='test_opt_subcommand.py', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'parser_name': 'q', 'self': _SubParsersAction(option_strings=[], dest='command', nargs='A?..', const=None, default='qux', type=None, choices={'lux': ArgumentParser(prog='my program lux', usage=None, description='long desc of lux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'qux': ArgumentParser(prog='my program qux', usage=None, description='long desc of qux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'bux': ArgumentParser(prog='my program bux', usage=None, description='long desc of bux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True), 'fux': ArgumentParser(prog='my program fux', usage=None, description='long desc of fux', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)}, help='some help on group A', metavar=None), 'values': 'qux'}] usage: test_opt_subcommand.py [-h] [-d] {lux,qux,bux,fux} ... test_opt_subcommand.py: error: argument command: unknown parser 'q' (choices: lux, qux, bux, fux) ERROR ====================================================================== ERROR: test_optional_subcommand_behavior (__main__.TestSubparserDefaultFun) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_opt_subcommand.py", line 32, in test_optional_subcommand_behavior self.parser.parse_args(prep(' ')) File "/home/bewest/Documents/git/argparse/argparse.py", line 1715, in parse_args args, argv = self.parse_known_args(args, namespace) File "/home/bewest/Documents/git/argparse/argparse.py", line 1754, in parse_known_args self.error(str(err)) File "/home/bewest/Documents/git/argparse/argparse.py", line 2398, in error self.exit(2, _('%s: error: %s\n') % (self.prog, message)) File "/home/bewest/Documents/git/argparse/argparse.py", line 2386, in exit _sys.exit(status) SystemExit: 2 ---------------------------------------------------------------------- Ran 1 test in 0.013s FAILED (errors=1) bewest@ripen:~/Documents/git/gist/cli-hello$ git diff bewest@ripen:~/Documents/git/gist/cli-hello$ git remote origin bewest@ripen:~/Documents/git/gist/cli-hello$ git remote add prune rename rm set-head show update bewest@ripen:~/Documents/git/gist/cli-hello$ git remote show -v error: unknown switch `v' usage: git remote show [<options>] <name> -n do not query remotes bewest@ripen:~/Documents/git/gist/cli-hello$ git remote -v origin [email protected]:1202975.git (fetch) origin [email protected]:1202975.git (push) bewest@ripen:~/Documents/git/gist/cli-hello$ j^C bewest@ripen:~/Documents/git/gist/cli-hello$ jobs bewest@ripen:~/Documents/git/gist/cli-hello$ ls a.py a.pyc b.py b.pyc README s.py test_opt_subcommand.py bewest@ripen:~/Documents/git/gist/cli-hello$
The trick is that parser types expect an array of values, and only check the first one. So to make it optional, the default must be couched in a list.
I think suppressing the default command should be supported as well. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Needs review and tests.
https://gist.github.com/1202975#file_test_opt_subcommand.py
https://gist.github.com/1202975#file_s.py