I'm trying to figure out how to add global option in a sub-parser scenario with pythons arparse library.
Right now my code looks like this:
def parseArgs(self):
parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument('--debug', default=False, required=False,
action='store_true', dest="debug", help='debug flag')
main_parser = argparse.ArgumentParser()
main_parser.add_argument('--debug', default=False, required=False,
action='store_true', dest="debug", help='debug flag')
service_subparsers = main_parser.add_subparsers(title="category",
dest="category")
agent_parser = service_subparsers.add_parser("agent",
help="agent commands", parents=[parent_parser])
return main_parser.parse_args()
This works for the command line ./test --help
and the --debug
option is listed as global:
usage: test [-h] [--debug] {agent} ... optional arguments: -h, --help show this help message and exit --debug debug flag category: {agent} agent agent commands
However when I trigger the agent sub-parser with the command line ./test agent --help
the --debug
option is now no longer listed as a global option but as an option for the sub-parser. Also it must now specified as ./test agent --debug
and ./test --debug agent
no longer works:
usage: test agent [-h] [--debug] optional arguments: -h, --help show this help message and exit --debug debug flag
What I'd like to be able to do is define --debug
is global so that it can always be specified for all sub-parsers and appropriately listed as such in the help output.
Aucun commentaire:
Enregistrer un commentaire