[英]Python: “AttributeError: 'Namespace' object has no attribute” argparse
[英]Argparse using subcommands/subparsers — AttributeError: 'Namespace' object has no attribute
我需要使用具有以下用法的 python 脚本:
script.py(命令)(选项)
我的问题是我如何为“命令”和“选项”添加 arguments?
我现在做的是这样的:
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: 'Namespace' object has no attribute '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 参数!
我对您的代码进行了一些更改; 希望它会澄清发生了什么:
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
中看到这样的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.