This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subparsers -> add_parser doesn't support hyphen char '-'
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: nir_barel@bmc.com, paul.j3, tryauuum
Priority: normal Keywords:

Created on 2018-07-04 16:19 by nir_barel@bmc.com, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg321057 - (view) Author: Nir Barel (nir_barel@bmc.com) Date: 2018-07-04 16:19
parent_parser = argparse.ArgumentParser(add_help=False)
main_parser = argparse.ArgumentParser(prog='app_cli')

service_subparsers = main_parser.add_subparsers(title="service",                                        dest="service_command", )

service_parser = service_subparsers.add_parser("-dr", help="sample help", parents=[parent_parser])

service_parser.add_argument("-old_host",help="my old host", dest="oldHost", required=False)

args = main_parser.parse_args()


when I try to run the app_cli -dr I am getting an error "error:unrecognized arguments: -dr"

when I try to run the app_cli with -dr -h I am getting the help of the main application and not the help of the service subparser

only if I change '-dr' to 'dr' it works as expected, but I need to support the hyphen for all arguments
msg322227 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-07-23 15:52
Do you understand why this is happening?

Subparsers is, in effect, a positional argument with 'choices' - the choices being the parsers (and their aliases).

But '-dr' looks like an flagged (optionals) argument.  Since you didn't define such an argument, it gets put in the 'unrecognized' category.

So, no, you can't used a flag-like name for a parser.  Why are you trying to do this?
msg405090 - (view) Author: Артём Иконников (tryauuum) Date: 2021-10-27 12:45
I've also struggled with this. The subparser documentation describes them as a tool to "split up functionality into a number of sub-commands". 
From the reader's point of view it's not obvious that such "sub-commands" cannot start with --
msg405107 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2021-10-27 16:12
Артём Иконников, developers and experienced users are familiar with other programs, such as 'svn' which is used as an example

svn checkout, svn update, and svn commit

Also the use of '--foo' as flagged/optional(s) argument is so familiar to developers, that it does nor occur us that new users might want to use that form here.  While there's no explicit prohibition of using the dash, non of the examples use it.  Examples are a good starting place when using new features.

If this problem came up more often, I'd recommend modifying the `add_parser` method to raise an error if the user provides a dashed command name (or alias).
History
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78227
2021-10-27 16:12:52paul.j3setmessages: + msg405107
2021-10-27 12:45:36tryauuumsetnosy: + tryauuum
messages: + msg405090
2018-09-23 02:34:39paul.j3setstatus: open -> closed
resolution: not a bug
stage: resolved
2018-07-23 15:52:15paul.j3setnosy: + paul.j3
messages: + msg322227
2018-07-04 16:19:59nir_barel@bmc.comcreate