Issue34046
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) * ![]() |
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) * ![]() |
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:02 | admin | set | github: 78227 |
| 2021-10-27 16:12:52 | paul.j3 | set | messages: + msg405107 |
| 2021-10-27 12:45:36 | tryauuum | set | nosy:
+ tryauuum messages: + msg405090 |
| 2018-09-23 02:34:39 | paul.j3 | set | status: open -> closed resolution: not a bug stage: resolved |
| 2018-07-23 15:52:15 | paul.j3 | set | nosy:
+ paul.j3 messages: + msg322227 |
| 2018-07-04 16:19:59 | nir_barel@bmc.com | create | |
