classification
Title: argparse silently ignores arguments
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Changaco, barry, gregory.p.smith, nailor, paul.j3, r.david.murray, remram
Priority: normal Keywords:

Created on 2014-12-15 21:02 by remram, last changed 2015-04-28 18:53 by barry. This issue is now closed.

Messages (6)
msg232679 - (view) Author: Rémi Rampin (remram) * Date: 2014-12-15 21:02
This works correctly on Python 3.4.

On Python 2.7, argparse seems to completely and silently ignore arguments in some conditions, for instance this setup will cause --verbose to be ignored on the main parser:

    options = argparse.ArgumentParser(add_help=False)
    options.add_argument('-v', '--verbose', action='store_true')
    parser = argparse.ArgumentParser(parents=[options])
    subparsers = parser.add_subparsers()
    parser_cmd = subparsers.add_parser('cmd', parents=[options])

Full runnable example here: http://paste.pound-python.org/show/XfVVhdJHSPISXLP1lASd/

Might or might not be related to #9351, workarounds welcome.
msg232682 - (view) Author: Rémi Rampin (remram) * Date: 2014-12-15 21:20
Interestingly, this worked before my upgrade 2.7.8 -> 2.7.9.
msg233040 - (view) Author: (Changaco) Date: 2014-12-23 10:48
honcho has been affected by this: https://github.com/nickstenning/honcho/issues/122

Working around the problem is possible but not very pretty: https://github.com/nickstenning/honcho/pull/121
msg233314 - (view) Author: Rémi Rampin (remram) * Date: 2015-01-02 16:12
I might use your workaround in ReproZip (https://github.com/ViDA-NYU/reprozip/issues/89), thanks. I agree that it doesn't look pretty...
msg239433 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-03-27 20:36
Without actually running this case I think it is caused by the  http://bugs.python.org/issue9351  patch.

You can check the __call__ method for class _SubParsersAction and how it handles invocation of the subparser.  Does it get the existing namespace, or a new one (None)?

There is an inherent ambiguity when the same argument ('dest' actually) is defined for both the parser and the subparser.  Doubly so when both get it via 'parents'.  Which default and which argument string has priority?

That other issue tried to fix the case where the subparser's default was being ignored, and in the process created a case where the parser's argument string is being ignored.

Wouldn't it be safer all around if the subparsers took different arguments, or at least different namespace 'dest', than the main parser?
msg242201 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2015-04-28 18:53
> Wouldn't it be safer all around if the subparsers took different arguments, or at least different namespace 'dest', than the main parser?

IMHO, yes.  I agree that the semantics of what the original code is trying to do is quite ambiguous.  Since the documentation says that parents= causes all the given parsers and adds their options to the parser being constructed, I'd take that to mean that the -v before the command, and the command's -v would point to the same dest.  The admonition for add_help=False seems to reinforce that.

The fact that this used to work should be considered an accident.  I wouldn't call it a regression because the documentation does not make it clear that this should work.  I think this is not a bug.

Feel free to reopen this if you disagree and and cite a convincing argument for sharing dests.  To be totally unambiguous, use different destinations.

FWIW, I've never used parents myself.  I've always done something like what honcho eventually landed.
History
Date User Action Args
2015-04-28 18:53:13barrysetstatus: open -> closed
resolution: not a bug
messages: + msg242201
2015-04-28 18:39:26barrysetnosy: + barry
2015-03-31 18:28:59berker.peksaglinkissue23555 superseder
2015-03-27 20:36:04paul.j3setmessages: + msg239433
2015-03-27 20:18:29paul.j3setnosy: + paul.j3
2015-03-23 23:15:26gregory.p.smithsetnosy: + r.david.murray, nailor

versions: + Python 3.4, Python 3.5
2015-03-23 22:04:30gregory.p.smithsetnosy: + gregory.p.smith
2015-01-02 16:12:43remramsetmessages: + msg233314
2014-12-23 10:48:50Changacosetnosy: + Changaco
messages: + msg233040
2014-12-15 21:20:51remramsetmessages: + msg232682
2014-12-15 21:02:07remramcreate