【问题标题】:Select limited arguments from Parent Parser从父解析器中选择有限的参数
【发布时间】:2020-12-30 20:11:59
【问题描述】:

我使用 Argparse 已经有一段时间了,这是我遇到的问题的 StackOverflow Answer

Add arguments to multiple subparsers

这个答案并没有完全解决我的问题。

这是从答案中借用的已编辑代码。 (我在添加换行符之前添加了注释)

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")

编辑后的代码代表这个

  • -p & -q 作为父参数

问题是,我不想在我的子解析器中使用新的父参数“-q”。

我只想在任何子解析器中使用参数“-p”。

这听起来有点不同,但由于我要处理这么多子解析器,我真的想要我的子解析器的最佳选择。

我应该怎么做?

【问题讨论】:

  • 链接的 SO 中接受的答案是旧的且有问题的。较新的答案更好。

标签: python python-3.x arguments argparse subparsers


【解决方案1】:

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

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

这是你想要的结果吗?

【讨论】:

    【解决方案2】:

    过去 SO 问题中关于 parentsubparsers 的一些要点很明显。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 2020-09-26
      • 2021-03-31
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多