I want the following usege:
我想要以下用途:
Usage 1: my_launch.py --list-installed
Usage 2: my_launch.py [-a | -b | -c <arg_c>] <appname [args ...]>
用法1:my_Launch.py--list-已安装用法2:my_Launch.py[-a|-b|-c]
Is that possible to combine with argparse?
这可以与argparse结合使用吗?
If the --list-installed
is used, then there should not be any other arguments.
In another case, there is required appname, and then zero or more arguments.
如果使用--list-Installed,则不应该有任何其他参数。在另一种情况下,有必需的appname,然后是零个或多个参数。
I have read about subparser, but they are not appropriate for my case, because they require the command to start from defined word (a subcommand).
我读过关于子解析器的文章,但它们不适合我的情况,因为它们要求命令从定义的单词(子命令)开始。
I thought I could just collect everything in namespace, and then do my own logic. But the problem is that I cannot make an appname with its parameters captured. In usage 2 it is mandatory, in usage 1 it is prohibited.
我以为我可以只收集名称空间中的所有内容,然后进行我自己的逻辑。但问题是,我无法使用捕获的参数创建appname。在用法2中它是强制的,在用法1中它是被禁止的。
Is it possible to make appname to collect all args behind it (I do it like this: parser.add_argument("appname", nargs=argparse.REMAINDER)
), and at the same time make the whole appname optional?
是否可以让appname收集它后面的所有参数(我是这样做的:parser.addargument(“appname”,nargs=argparse.REMAINDER),同时使整个appname成为可选的?
I also read about mutually exclusive groups, but the term is ambiguous. Actually, they are not "groups", but a set from which only one can be used. But I wanted two actually groups, one group for usage 1, another group for usage 2. And make them mutually exclusive. That is why I used "para" in question.
我也读到过相互排斥的群体,但这个词很含糊。事实上,它们不是“组”,而是一个只能使用一个组的集合。但我想要两个实际的组,一个组用于用法1,另一个组用于用法2。并使它们相互排斥。这就是为什么我用了“para”这个词。
更多回答
Unbelievable. I have found a solution right after I posted the question, but made several hours research before :(. I can use parser.add_argument("appname", default=None, nargs=argparse.REMAINDER)
. From here stackoverflow.com/a/4480202/7869636. I thoughe that you need to respecify nargs
, while it has been already specifically specified. Still, maybe someone suggest the beautiful solution using the argparse's logic.
难以置信。我在发布问题后就找到了解决方案,但在几个小时前进行了研究:我可以使用parser.addargument(“appname”,默认=无,nargs=argparse.REMAINDER)。从这里开始,stackoverflow.com/a/4480202/7869636。我认为您需要重新指定Nargs,因为它已经被特别指定了。尽管如此,也许有人利用argparse的逻辑提出了一个漂亮的解决方案。
a/b/c
can work as A mutually_exclusive_group
. While you can have separate groups, they can't be nested. REMAINDER` ('...') is like *
but takes all the rest. '--' may also be useful. REMAINDER` is not currently documented because there are some problematic edge cases, but it is useable.
A/b/c可以作为A互斥组工作。虽然您可以拥有单独的组,但它们不能嵌套。REMAINDER`(‘...’)就像*,但把剩下的都拿走了。‘--’可能也很有用。REMAINDER`目前没有文档,因为有一些有问题的边缘情况,但它是可用的。
Why argparse
cannot nest (para?) groups? It is the standard argument parser library, and I am surprised that it is incomplete. --
is not applicable for my case. Thanks for mentioning the reason why it is not documented. I also was surprised about that.
为什么argparse无法嵌套(第?段?)团体?它是标准的参数解析器库,我很惊讶它是不完整的。--不适用于我的情况。谢谢你提到它没有被记录下来的原因。我也对此感到惊讶。
I worked on a patch to handle nested groups (with all logical possibilities) years ago. Setup wasn't a problem, performing the tests not too hard. Formatting the usage required a big rewrite. That and other group issues can be found the github repository.
几年前,我开发了一个补丁来处理嵌套组(具有所有合理的可能性)。设置不是问题,执行测试不是太难。格式化用法需要大量重写。这个问题和其他群组问题可以在GitHub存储库中找到。
I would be ok to just be able to use that logic. If it cant handle formatting the usage, I could write it manually. Just like I did in this question, I just wrote two separate usage lines. Maybe it is possible to combine them in one, but it is not really needed, because readability is better when they are separate.
我只要能够使用这种逻辑就可以了。如果它不能处理格式化用法,我可以手动编写。就像我在这个问题中所做的那样,我只写了两个单独的用法行。也许可以将它们组合在一起,但实际上并不需要,因为当它们分开时可读性更好。
我是一名优秀的程序员,十分优秀!