用于组合其他选项的 Argparse 快捷选项 [英] Argparse shortcut option for combining other options
问题描述
假设我有一个输入文件选项和一个输出文件选项.我怎样才能创建一个结合这两者的选项?例如:
Let's say I have an input file option and an output file option. How can I create an option that combines the two? For example:
$ ./my_script.py -i input.txt -o output.txt
可以组合为:
$ ./my_script.py --io input_output.txt
你可能会说我可以用 -io
来组合这两个选项,但是 -io filename
是 -i -o filename
的快捷方式>,不是 -i 文件名 -o 文件名
.
You might say that I could do -io
to combine both options, but -io filename
is a shortcut for -i -o filename
, not -i filename -o filename
.
我认为可以将 dest=('input', 'output')
添加到我的 .add_argument()
调用中,但这引发了错误dest
必须是一个字符串.
I thought that it might be possible to add dest=('input', 'output')
to my .add_argument()
call, but that raised an error that dest
must be a string.
我尝试在一侧添加一个带有 --io
的互斥组,在另一侧添加一组 -i
和 -o
一边,但是当程序使用 --help
运行时,-i
和 -o
的帮助文本不再显示:
I tried adding a mutually exclusive group with --io
on one side and a group of -i
and -o
on the other side, but the help texts for -i
and -o
did not show up any more when the program was run with --help
:
usage: myscript.py [-h] [--io] [-i INPUT] [-o OUTPUT]
optional arguments:
-h, --help show this help message and exit
--io Use file as both input and output
此外,互斥的部分似乎不起作用.我仍然可以使用 --io
和单独的 -i
和 -o
选项调用脚本,并且不会引发错误.
Also, the mutually exclusive part didn't seem to work. I am still allowed to call the script with --io
and the individual -i
and -o
options, and no error is raised.
这是我的骨架代码:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="Input file")
parser.add_argument("-o", "--output", help="Output file")
parser.parse_args()
带组的代码:
import argparse
parser = argparse.ArgumentParser()
exclusive = parser.add_mutually_exclusive_group()
exclusive.add_argument("--io", help="Input and Output")
sub = exclusive.add_argument_group()
sub.add_argument("-i", "--input", help="Input file")
sub.add_argument("-o", "--output", help="Output file")
args = parser.parse_args()
if args.io:
in_file = out_file = args.io
else:
in_file = args.input
out_file = args.output
推荐答案
解释为什么事情不起作用比提出解决方案更容易.
It is easier to explain why things don't work than to suggest a solution.
是的,dest
必须是字符串;没有提供 dest
值的列表或元组.但是您的 in_file = out_file = args.io
很好地解决了这个问题.您也可以使用:
Yes, dest
must be a string; there's no provision for a list or tuple of dest
values. But your in_file = out_file = args.io
addresses that issue just fine. You could have also used:
args.in_file=args.out_file = args.io
在解析后按摩 args
值没有任何问题.
There's nothing wrong with massaging the args
values after parsing.
argument_group
不是为嵌套而设计的,也不是向 mutually_exclusive_group
添加任何"(或与")逻辑的方式.也许在遥远的未来会有一种定义完整逻辑组合的方法,但现在不行.其实做测试并不难;很难定义 API 和使用格式.
argument_group
is not designed for nesting, nor is it a way of adding 'any' (or 'and') logic to the mutually_exclusive_group
. Maybe in the distant future there will be a way of defining a full set of logical combinations, but not now. Actually it isn't hard to do the tests; it's hard to define the API and the usage formatting.
还请记住,mutually_exclusive_group
用于格式化参数的使用和测试 co_ocurrance,而 argument_group
用于对参数帮助行进行分组.两个截然不同的目的.
Also keep in mind that mutually_exclusive_group
is used to format the usage and test for co_ocurrance of arguments, while argument_group
is used to group argument help lines. Two very different purposes.
如果 -i
是 store_true
参数,则 -io filename
将被理解为 -i -o filename
.但是也翻译一下 -i filename -o filename
不在当前代码中(并且可能不常见到需要补丁).
If -i
was a store_true
argument then -io filename
would be understood as -i -o filename
. But translating it too -i filename -o filename
is not in the current code (and probably not common enough to warrant a patch).
如果你还想使用-i
、-o
和--io
(我更喜欢--
对于 2 个字符标志)我可以建议一些事情:
If you still want to use -i
, -o
and --io
(I prefer --
for 2 character flags) I can suggest a couple of things:
编写一个自定义用法来演示您想要的内容.如果很难写出清晰的用法,那么你的设计可能太复杂了.
write a custom usage that demonstrates what you want. If it is hard to write a clear usage, then your design is probably too complicated.
在解析后做你自己的exclusive groups
测试.args.in_file is None
是测试是否使用标志的好方法.另一种可能性是定义默认值,这样您就不必关心用户使用哪种组合.
do your own exclusive groups
testing after parsing. args.in_file is None
is a good way of testing whether a flag has been used or not. Another possibility is to define defaults such that you don't care which combination the user uses.
这篇关于用于组合其他选项的 Argparse 快捷选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!