不能有多个子解析器参数 [英] cannot have multiple subparser arguments
问题描述
所以我正在尝试使用以下模式构建 CLI:
So i'm trying to build an CLI with the following pattern:
cli.py api new --config config.json
或
cli.py api del [api_name]
为了实现 api
,我将它添加为 sup 解析器
To achieve the api
i've added it as sup parser
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='my prog')
subparsers = parser.add_subparsers(title='api', help='available actions')
api_parser = subparsers.add_parser('api')
从这里我想我们可能想要添加两个新的子解析器来处理 new
和 del
子命令:
from here i thought that we might want to add two new subparsers to handle the new
and del
subcommands:
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='my prog')
subparsers = parser.add_subparsers(title='api', help='available actions')
api_parser = subparsers.add_parser('api')
api_new_subparsers = api_parser.add_subparsers(title='new', help='%(prog)s creates new api gateway')
api_del_subparsers = api_parser.add_subparsers(title='del', help='%(prog)s deletes an api gateway')
但是我得到了错误:cannot have multiple subparser arguments
我搜索了一下.大部分问题都是关于下面的模式cli.py cmdA
和cli.py cmdB
.所以我开始认为 argparse 可能无法达到这种深度"?
I've searched a bit. Most of the questions are about the following pattenr cli.py cmdA
and cli.py cmdB
. So i started thinking that argparse maybe is not able to achieve such kind of "depth"?
非常感谢.
推荐答案
首先,我不确定您是否理解 title
参数的用途.
First, I'm not sure you understand the purpose of the title
parameter.
In [332]: parser = argparse.ArgumentParser(prog='my_prog')
...: subparsers = parser.add_subparsers(title='api', help='available actions')
...: api_parser = subparsers.add_parser('api')
...:
In [333]: parser.print_help()
usage: my_prog [-h] {api} ...
optional arguments:
-h, --help show this help message and exit
api:
{api} available actions
In [334]: parser.parse_args('api'.split())
Out[334]: Namespace()
title
所做的就是在帮助中定义一个组.它不会改变解析.定义 dest
而不是 title
(或除此之外):
All title
does is define a group in the help. It doesn't change parsing. Defining a dest
instead of title
(or in addition to):
In [335]: parser = argparse.ArgumentParser(prog='my_prog')
...: subparsers = parser.add_subparsers(dest='cmd', help='available actions')
...: api_parser = subparsers.add_parser('api')
...:
In [336]: parser.print_help()
usage: my_prog [-h] {api} ...
positional arguments:
{api} available actions
optional arguments:
-h, --help show this help message and exit
In [337]: parser.parse_args('api'.split())
Out[337]: Namespace(cmd='api')
这标识了 args
命名空间中的子解析器命令.
This identifies the subparser command in the args
namespace.
但是关于多个子解析器的问题 - argparse
不允许您定义它.你可以嵌套它们.那是你可以使用的
But on to the question of multiple subparsers - argparse
does not allow you to define that. You can nest them. That is you could use
sp2 = api_parser.add_subparsers(dest='foo')
sp2.add_parser('del')
sp2.add_parser('inc')
In [339]: parser.parse_args('api del'.split())
Out[339]: Namespace(cmd='api', foo='del')
In [340]: parser.parse_args('api inc'.split())
Out[340]: Namespace(cmd='api', foo='inc')
In [341]: parser.parse_args('api -h'.split())
usage: my_prog api [-h] {del,inc} ...
positional arguments:
{del,inc}
但在您朝那个方向走得太远之前,您是否尝试过在 api_parser
中添加简单的参数?使用子解析器越深入,帮助显示就越混乱.
But before you get too far in that direction, have you tried adding plain arguments the api_parser
? Help display gets messy the deeper you go with subparsers.
或者您可能不需要完整的子解析器机制.例如用选择定义两个位置:
Or may be you don't need the full subparser mechanism. For example defining two positionals with choices:
In [345]: parser = argparse.ArgumentParser(prog='my_prog')
...: parser.add_argument('cmd1', choices=['api'], help='available actions')
...: ;
...: parser.add_argument('cmd2', choices=['del','inc']);
...:
In [347]: parser.parse_args('api inc'.split())
Out[347]: Namespace(cmd1='api', cmd2='inc')
In [348]: parser.parse_args('-h'.split())
usage: my_prog [-h] {api} {del,inc}
positional arguments:
{api} available actions
{del,inc}
这篇关于不能有多个子解析器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!