Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从父解析器中选择有限参数_Python_Python 3.x_Arguments_Argparse_Subparsers - Fatal编程技术网

Python 从父解析器中选择有限参数

Python 从父解析器中选择有限参数,python,python-3.x,arguments,argparse,subparsers,Python,Python 3.x,Arguments,Argparse,Subparsers,我已经与Argparse合作了一段时间,下面是我遇到的问题的答案 这个答案并不能完全解决我的问题 下面是从答案中借用的经过编辑的代码。 (我在添加新行之前添加了一条评论) 编辑后的代码表示这一点 -p&-q作为父参数 问题是,我不想在我的子parser中使用新的父参数'-q' 我只想在任何子parser中使用参数'-p' 这听起来有点不同,但由于我正在处理这么多的次级投资者,我真的希望为我的次级投资者提供最好的选择 我应该怎么做呢?据我所知,您可以在创建子解析器后向父解析器添加参数。所以你

我已经与Argparse合作了一段时间,下面是我遇到的问题的答案

这个答案并不能完全解决我的问题

下面是从答案中借用的经过编辑的代码。 (我在添加新行之前添加了一条评论)

编辑后的代码表示这一点

  • -p&-q作为父参数
问题是,我不想在我的子parser中使用新的父参数'-q'

我只想在任何子parser中使用参数'-p'

这听起来有点不同,但由于我正在处理这么多的次级投资者,我真的希望为我的次级投资者提供最好的选择


我应该怎么做呢?

据我所知,您可以在创建子解析器后向父解析器添加参数。所以你可以这样做:

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")
然后,您将获得以下帮助:

对于父解析器 为下级军官
这就是你想要的结果吗?

一些在过去很明显的问题是关于
父级
子级

  • 不要在主参数和子参数中定义相同的参数(
    dest
  • 因此,不要将主解析器用作父解析器
  • 您可以在
    parents
    列表中定义并传递多个解析器
  • 家长
    只是一个方便的工具。如果它给你带来问题(或者你不理解它),不要使用它
  • 编写自己的实用程序函数向子parser添加参数很容易。这些将更容易定制
  • argparse
    是一个python模块,定义python类。在有用的地方使用基本的python思维

链接SO中的公认答案既陈旧又有问题。更新的答案更好。
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")
>>> 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
>>> 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