[New-bugs-announce] [issue34479] ArgumentParser subparser error display at the wrong level
Siming Yuan
report at bugs.python.org
Thu Aug 23 11:32:12 EDT 2018
New submission from Siming Yuan <siming85 at gmail.com>:
If you take the example from
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_subparsers
# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')
# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('-bar', type=int, help='bar help')
# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('--baz', choices='XYZ', help='baz help')
and run a subcommand but with an argument the subcommand doesn't understand
parser.parse_args(['a', '-x'])
the output doesn't help much - because the usage is coming from the main parser
usage: PROG [-h] [--foo] {a,b} ...
PROG: error: unrecognized arguments: -x
the reason for failure is because the error api being called in this case is
parser.error(msg)
not the subparser
parser_a.error(msg)
the proper error should've been
usage: PROG a [-h] [-bar BAR]
PROG a: error: unrecognized arguments: -x
----------
components: Library (Lib)
messages: 323956
nosy: siming85
priority: normal
severity: normal
status: open
title: ArgumentParser subparser error display at the wrong level
type: behavior
versions: Python 3.4, Python 3.5, Python 3.6
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34479>
_______________________________________
More information about the New-bugs-announce
mailing list