[issue34479] ArgumentParser subparser error display at the wrong level

2018-09-22 Thread paul j3


Change by paul j3 :


--
resolution:  -> not a bug

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34479] ArgumentParser subparser error display at the wrong level

2018-09-20 Thread paul j3


Change by paul j3 :


--
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34479] ArgumentParser subparser error display at the wrong level

2018-08-28 Thread paul j3


paul j3  added the comment:

Errors that are associated with a specific argument, such as a wrong 'type' do 
get usage from the appropriate subparser.  

e.g.

In [164]: p.parse_args('--foo 1 cmd1 --bar x'.split())
usage: ipython3 cmd1 [-h] [--bar BAR]
ipython3 cmd1: error: argument --bar: invalid int value: 'x'

`required` tests also issue subparser specific usage; mutually exclusive tests 
probably do so as well.

But this unrecognized argument error is a bit less specific.  In your example 
'-x a' and 'a -x' will both produce the same error message.  The route by which 
the '-x' is put into the 'extras' list is different in the two cases, but in 
both it's the top 'parse_(known_)args' that determines whether to just return 
them, or raise an error.  '-x a -x' will put 2 '-x' in the unrecognized list.

If you really need a subparser specific message it might be possible to do so 
by modifying, or subclassing the _SubParsersAction class, making it raise an 
error when there are 'extras' rather than returning them as 'unrecognized'.  
But that's not a backward compatible change.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34479] ArgumentParser subparser error display at the wrong level

2018-08-28 Thread paul j3


paul j3  added the comment:

The subparser is called with `parse_known_args` which just puts unknown args in 
a _UNRECOGNIZED_ARGS_ATTR attribute slot in the namespace, which is then passed 
back to the main parser.

(this occurs in _SubParsersAction.__call__)

If `parser.parse_known_args` is used, then these arguments will appear in the 
`extras` list.  Otherwise `parser.parse_args` issues the 'unrecognized 
arguments' error message (with the main parser usage).

I suspect this issue has been raised previously.  And I don't see any easy way 
of altering the behavior.

--
nosy: +paul.j3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34479] ArgumentParser subparser error display at the wrong level

2018-08-23 Thread Siming Yuan


New submission from Siming Yuan :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com