Skip to content
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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

bewest
Copy link
Owner

@bewest bewest commented Oct 2, 2011

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.
@bewest
Copy link
Owner Author

bewest commented Oct 2, 2011

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant