小编Pet*_*kel的帖子

不要在argparse的print_help()中显示两次长选项

我有以下代码:

parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool',
        prog='pqa',
        usage='%(prog)s [-h] [-v,--version]')
parser.add_argument('-l', '--list', action='store_true',
        help='Shows full overview of all queues')
parser.add_argument('-q', '--queue', action='store', metavar='<queue>', dest='queue',
        help='Show information for <queue>')
parser.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain',
        help='Show information about a specific <domain>')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

这给了我这样的输出:

%./pqa                                                                                                                        
usage: pqa [-h] [-v,--version]

Postfix Queue Administration Tool

optional arguments:
  -h, --help            show this help message and exit
  -l, --list            Shows full overview of all queues
  -q <queue>, --queue <queue> …
Run Code Online (Sandbox Code Playgroud)

python argparse

13
推荐指数
2
解决办法
3164
查看次数

检查命名空间中是否存在变量

我正在尝试使用 argparse 的输出(简单的 argparse 只有 4 个位置参数,函数的每次启动都取决于设置为 True 的变量)

Namespace(battery=False, cache=True, health=False, hotspare=False)
Run Code Online (Sandbox Code Playgroud)

目前,我正在尝试找出如何最好地要求 python 查看这些变量之一何时设置为 True;不必像我现在那样进行硬编码:

if args.battery is True:
    do_something()
elif args.cache is True:
    do_something_else()
etc ...
Run Code Online (Sandbox Code Playgroud)

我宁愿只使用一个命令来检查命名空间中是否存在变量以及它是否设置为 True;但我一生都无法弄清楚如何以有效的方式做到这一点。

python namespaces argparse

11
推荐指数
2
解决办法
1万
查看次数

标签 统计

argparse ×2

python ×2

namespaces ×1