简体   繁体   中英

Select limited arguments from Parent Parser

I have been working with Argparse for a while and here's the StackOverflow Answer to the question that I was having.

Add arguments to multiple subparsers

This answer is not completely solving my problem.

Here's the edited code borrowed from the answer. (I have added a comment before adding the newline)

import argparse

parent_parser = argparse.ArgumentParser(description="The parent parser")
parent_parser.add_argument("-p", type=int, required=True,
                           help="set db parameter")

#adding a new parent argument
parent_parser.add_argument("-q", type=int, required=True,
                           help="help with -q")


subparsers = parent_parser.add_subparsers(title="actions")
parser_create = subparsers.add_parser("create", parents=[parent_parser],
                                      add_help=False,
                                      description="The create parser",
                                      help="create the orbix environment")
parser_create.add_argument("--name", help="name of the environment")
parser_update = subparsers.add_parser("update", parents=[parent_parser],
                                      add_help=False,
                                      description="The update parser",
                                      help="update the orbix environment")

The edited code represents this

  • -p & -q as the Parent Arguments

The problem is, I dont want to use that the new Parent Argument '-q' in my subparser.

I just want to use the argument '-p' in any of the subparsers.

It sounds a little different, but as I'm dealing with so many subparsers, I really want the best option for my subparsers.

What should I do for that?

From my understanding, you can add arguments to the parent parser after having created subparsers. So you can do like this :

import argparse

parent_parser = argparse.ArgumentParser(description="The parent parser")
parent_parser.add_argument("-p", type=int, required=True,
                           help="set db parameter")

# Not adding now


subparsers = parent_parser.add_subparsers(title="actions")
parser_create = subparsers.add_parser("create", parents=[parent_parser],
                                      add_help=False,
                                      description="The create parser",
                                      help="create the orbix environment")
parser_create.add_argument("--name", help="name of the environment")
parser_update = subparsers.add_parser("update", parents=[parent_parser],
                                      add_help=False,
                                      description="The update parser",
                                      help="update the orbix environment")

#adding a new parent argument
parent_parser.add_argument("-q", type=int, required=True,
                           help="help with -q")

You then have the following helps :

For the parent parser

>>> parent_parser.print_help()
usage: temp.py [-h] -p P -q Q {create,update} ...

The parent parser

optional arguments:
  -h, --help       show this help message and exit
  -p P             set db parameter
  -q Q             help with -q

actions:
  {create,update}
    create         create the orbix environment
    update         update the orbix environment

For the subparsers

>>> parser_create.print_help()
usage: temp.py create [-h] -p P [--name NAME] {create,update} ...

The create parser

optional arguments:
  -h, --help       show this help message and exit
  -p P             set db parameter
  --name NAME      name of the environment
# no q here

actions:
  {create,update}
    create         create the orbix environment
    update         update the orbix environment

>>> parser_update.print_help()
usage: temp.py update [-h] -p P {create,update} ...

The update parser

optional arguments:
  -h, --help       show this help message and exit
  -p P             set db parameter
# no -q here neither
actions:
  {create,update}
    create         create the orbix environment
    update         update the orbix environment

Is that the result you want ?

Some points that are apparent in past SO questions about parent and subparsers .

  • don't define the same arguments ( dest ) in the main and subparsers
  • as a consequence, don't use the main parser as a parent.
  • you can define and pass multiple parsers in the parents list.
  • parents is just a convenience tool. Don't use it if it gives you problems (or you don't understand it).
  • it's easy to write your own utility function(s) to add arguments to subparsers. These will be easier to customize.
  • argparse is a python module, defining python classes. Use basic python thinking where useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM