paul j3 <ajipa...@gmail.com> added the comment:
It's the subparser that's producing this error, specifically its 'prog' attribute. If I use a custom usage with a simple parser: 1129:~/mypy$ python3 issue42297.py --foo=on usage: issue42297.py one two three issue42297.py: error: argument --foo: ignored explicit argument 'on' Notice that the error line includes the 'prog'. With subparsers, the main usage is included in the subcommand prog: print(subcmd_parser.prog) produces: test usage string ending in newlines foo --bar foo --bar foo --bar foo That's the usage plus the subcommand name, 'foo'. Generating the explicit error in the subcommand: 1244:~/mypy$ python3 issue42297.py foo --bar=on test usage string ending in newlines foo --bar foo --bar foo --bar foo: error: argument --bar: ignored explicit argument 'on' 'issue42297.py: ' has been replaced by the usage+'foo', and no newline. We don't see this in the 'unrecognized' case because that error issued by the main parser. issue42297.py: error: unrecognized arguments: on If I explicitly set the prog of the subcommand: subcmd_parser = subparser.add_parser('foo', prog='myscript foo') The error becomes: 1256:~/mypy$ python3 issue42297.py foo --bar=on usage: myscript foo [-h] [--bar] myscript foo: error: argument --bar: ignored explicit argument 'on' I can also add 'usage=usage_string' to the add_parser. For the most part add_parser takes the same parameters as ArgumentParser. Alternatively we can specify prog in subparser = parser.add_subparsers(dest='subcmd', metavar='subcmd', prog='myscript') resulting in: myscript foo: error: argument --bar: ignored explicit argument 'on' I recently explored how 'prog' is set with subparsers in https://bugs.python.org/issue41980 I don't think anything needs to be corrected in argparse. There are enough options for setting prog and usage in subcommands to get around this issue. In the worse case, you might want to create an alternative _SubParsersAction Action subclass that defines the prog/usage differently. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue42297> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com