0

我使用 Argparse 已经有一段时间了,这里是StackOverflow对我遇到的问题的回答。

向多个子解析器添加参数

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

这是从答案中借来的编辑后的代码。(我在添加换行符之前添加了评论)

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”。

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

我该怎么做?

4

2 回答 2

0

parent在过去关于和的 SO 问题中很明显的一些要点subparsers

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

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

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

那是你想要的结果吗?

于 2020-09-12T15:24:55.787 回答