Argparse使用子命令/子分析器-AttributeError:“名称空间”对象没有属性

时间:2020-07-03 13:33:07

标签: python arguments argparse

我需要使用具有以下用法的python脚本:

script.py(命令)(选项)

我的问题是如何为“命令”和“选项”添加参数?

我现在所做的是:

parser = argparse.ArgumentParser()
parser._optionals.title = "Options"
parser.add_argument('-help','--help', action="store_true", dest="help", help='help')
subparsers = parser.add_subparsers(help="All available commands", title="Commands")

parser_start = subparsers.add_parser('start', help='Starts the script', add_help=False)
parser_start._optionals.title = "Options"
parser_start.add_argument('--help', action="store_true", dest="help_start")
parser_start.add_argument('-f', type=str, dest="file", help='simulation file to start')

parser_ls = subparsers.add_parser('ls', help='Lists running simulations', add_help=False)
parser_ls._optionals.title = "Options"
parser_ls.add_argument('--help', action="store_true", dest="help_ls")
parser_ls.add_argument('--all', action="store_true", help='Display all simulations')

parser_stop = subparsers.add_parser('stop', help='Stops simulation', add_help=False)
parser_stop._optionals.title = "Options"
parser_stop.add_argument('--help', action="store_true", dest="help_down")
parser_stop.add_argument('--sim-name', type=str, dest="sim_name")

args = parser.parse_args()

如果我尝试访问args.help_up,则会收到: AttributeError:“名称空间”对象没有属性“ help_start”

如何将parser_up,parser_stop和parser_ls传递给parse_args? 然后我该如何访问它们?

目标是要具有自定义帮助消息(我有atm,这就是我禁用帮助的原因),并像这样运行脚本

script.py start -f (name of file)
script.py stop --sim-name (name of simulation)

编辑:

如果我添加 args2 = parser_start.parse_args(),则可以读取args2.help_start,但找不到任何start,ls或down参数! / p>

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改;希望它将澄清正在发生的事情:

import argparse

parser = argparse.ArgumentParser()
parser._optionals.title = "Options"
#parser.add_argument('-help','--help', action="store_true", dest="help", help='help')
# conflicts with original help
subparsers = parser.add_subparsers(help="All available commands", title="Commands",
       dest='cmd')    # NEW

parser_start = subparsers.add_parser('start', help='Starts the script', add_help=False)
parser_start._optionals.title = "Options"
parser_start.add_argument('--help', action="store_true", dest="help_start")
parser_start.add_argument('-f', type=str, dest="file", help='simulation file to start')

parser_ls = subparsers.add_parser('ls', help='Lists running simulations', add_help=False)
parser_ls._optionals.title = "Options"
parser_ls.add_argument('--help', action="store_true", dest="help_ls")
parser_ls.add_argument('--all', action="store_true", help='Display all simulations')

parser_stop = subparsers.add_parser('stop', help='Stops simulation', add_help=False)
parser_stop._optionals.title = "Options"
parser_stop.add_argument('--help', action="store_true", dest="help_down")
parser_stop.add_argument('--sim-name', type=str, dest="sim_name")

args = parser.parse_args()

print(args)      # NEW

并运行示例:

0939:~/mypy$ python3 stack62716530.py 
Namespace(cmd=None)
0939:~/mypy$ python3 stack62716530.py --help
usage: stack62716530.py [-h] {start,ls,stop} ...

Options:
  -h, --help       show this help message and exit

Commands:
  {start,ls,stop}  All available commands
    start          Starts the script
    ls             Lists running simulations
    stop           Stops simulation
0939:~/mypy$ python3 stack62716530.py start
Namespace(cmd='start', file=None, help_start=False)
0939:~/mypy$ python3 stack62716530.py start --help
Namespace(cmd='start', file=None, help_start=True)

如果我添加:

if getattr(args, 'help_start',False):
    parser_start.print_help()

我明白了

0940:~/mypy$ python3 stack62716530.py start --help
Namespace(cmd='start', file=None, help_start=True)
usage: stack62716530.py start [--help] [-f FILE]

Options:
  --help
  -f FILE  simulation file to start

关键是,只有调用help_start子解析器时,start才是属性。

在您的代码的第一次读取中,我错过了dest='help_start'参数。因此,我无法告诉您您为什么期望在args中看到这样的属性。