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

Created on 2018-07-04 16:19 by, last changed 2018-09-23 02:34 by paul.j3. This issue is now closed.

Messages (2)
msg321057 - (view) Author: Nir Barel ( 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?
Date User Action Args
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