Argparse快捷方式选项,用于组合其他选项 [英] Argparse shortcut option for combining other options
问题描述
假设我有一个输入文件选项和一个输出文件选项.如何创建将两者结合的选项?例如:
$ ./my_script.py -i input.txt -o output.txt
可以组合为:
$ ./my_script.py --io input_output.txt
您可能会说我可以做 -io
来组合两个选项,但是 -io filename
是 -i -o filename
的快捷方式>,而不是 -i filename -o filename
.
我认为可以在我的 .add_argument()
调用中添加 dest =('input','output')
dest
必须是字符串.
我尝试添加一个互斥的组,一侧为-io
,另一侧为一组 -i
和 -o
但是,当使用-help
运行程序时, -i
和 -o
的帮助文本不再显示./p>
用法:myscript.py [-h] [--io] [-i INPUT] [-o OUTPUT]可选参数:-h,--help显示此帮助消息并退出--io使用文件作为输入和输出
此外,互斥的部分似乎也不起作用.我仍然可以使用-io
以及单独的 -i
和 -o
选项调用脚本,并且不会出现错误./p>
这是我的基本代码:
import argparse解析器= argparse.ArgumentParser()parser.add_argument(-i","--input",help =输入文件")parser.add_argument(-o","--output",help =输出文件")parser.parse_args()
带有组的代码:
import argparse解析器= argparse.ArgumentParser()Exclusive = parser.add_mutually_exclusive_group()Exclusive.add_argument(-io",help =输入和输出")子= Exclusive.add_argument_group()sub.add_argument(-i","--input",help =输入文件")sub.add_argument(-o","--output",help =输出文件")args = parser.parse_args()如果是args.io:in_file = out_file = args.io别的:in_file = args.inputout_file = args.output
比起提出解决方案,更容易解释为什么事情不起作用.
是,目的地
必须是字符串;没有提供 dest
值的列表或元组.但是您的 in_file = out_file = args.io
地址可以很好地解决这个问题.您还可以使用:
args.in_file = args.out_file = args.io
在解析后按摩 args
值没有问题.
argument_group
并不是为嵌套而设计的,也不是将"any"(或"and")逻辑添加到 mutually_exclusive_group
的一种方式.也许在遥远的将来,将会有一种方法来定义一套完整的逻辑组合,但现在还不是.实际上,进行测试并不难.很难定义API和使用格式.
还请记住, mutually_exclusive_group
用于格式化用法并测试参数的共现性,而 argument_group
用于对参数帮助行进行分组.两个非常不同的目的.
如果 -i
是 store_true
自变量,则 -io文件名
将被理解为 -i -o文件名
.但是也要翻译它 -i filename -o filename
不在当前代码中(并且可能不够常见,不足以提供补丁).
如果您仍然想使用 -i
, -o
和-io
(我更喜欢使用-
对于2个字符标记),我可以建议几件事:
-
编写一个自定义用法来演示您想要的内容.如果很难编写清晰的用法,则您的设计可能太复杂了.
解析后, -
进行自己的
独占组
测试.args.in_file为None
是测试标记是否已使用的好方法.另一种可能性是定义默认值,这样您就不必在乎用户使用哪种组合.
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
could be combined as:
$ ./my_script.py --io input_output.txt
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
.
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.
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
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.
Here is my skeleton code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="Input file")
parser.add_argument("-o", "--output", help="Output file")
parser.parse_args()
The code with groups:
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.
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
There's nothing wrong with massaging the args
values after parsing.
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.
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.
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).
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.
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屋!