简体   繁体   English

Python:没有破折号的argparse可选参数

[英]Python: argparse optional arguments without dashes

I would like to have the following syntax: 我想有以下语法:

python utility.py file1 FILE1 file2 FILE2

where file1 and file2 are optional arguments. 其中file1和file2是可选参数。 It is simple to make it working with this syntax: 使用这种语法很简单:

python utility.py --file1 FILE1 --file2 FILE2

using 运用

parser.add_argument('--file1',type=file)
parser.add_argument('--file2',type=file)

however, if I remove the dashes, argparse starts to interprete it as a positional rather than optional argument... 但是,如果我删除破折号,argparse开始将其解释为位置而非可选参数......

In other words, is it possible to specifically tell argparse whether an argument is optional or positional so that I can have optional parameters without the dashes? 换句话说,是否可以专门告诉argparse一个参数是可选的还是位置的,这样我可以有没有破折号的可选参数?

There is no way to get argparse to do this for you. 没有办法让argparse为你做这件事。 However, you can make argparse accept any number of positional arguments: 但是,您可以使argparse接受任意数量的位置参数:

parser.add_argument('FILES',nargs='*')
options=parser.parse_args()
file1,optional_files=options.FILES[0],options.FILES[1:]

Of course, you may want to add some checks to make sure that at least 1 file was given, etc. 当然,您可能需要添加一些检查以确保至少提供了1个文件,等等。

EDIT 编辑

I'm still not 100% sure what you want here, but if file1 and file2 are literal strings, you can work around that a little bit by preprocessing sys.argv . 我仍然不能100%确定你想要什么,但如果file1file2是文字字符串,你可以通过预处理sys.argv来解决这个问题。 Of course, this will still format your help message strangely, but you can always add an epilog explaining that either form is OK: 当然,这仍然会奇怪地格式化您的帮助信息,但您总是可以添加一个epilog来解释任何一种形式都可以:

import argparse
import sys

mangle_args=('file1','file2')
arguments=['--'+arg if arg in mangle_args else arg for arg in sys.argv[1:]]

parser=argparse.ArgumentParser()
parser.add_argument('--file1')
parser.add_argument('--file2')
options=parser.parse_args(arguments)

Another Example would be: 另一个例子是:

train.py

import argparse
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Arguments for wake_word")
    parser.add_argument('data', type=str, help="path to data")
    parser.add_argument('output', type=str, help="model save path")
    parser.add_argument('batch_size', type=int, help="batch size")
    parser.add_argument('epochs', type=int, help="no.s of epochs")
    args = parser.parse_args()
print(args.data + args.output + args.batch_size + args.epochs)

then you can just run this code with arguments without dash 那么你可以用没有破折号的参数运行这段代码

train.py /path/to/data/ /path/to/output_files/ 128 100

And, in ascending order 并且,按升序排列

Had same problem. 有同样的问题。 My workaround is: 我的解决方法是:

lastarg = sys.argv[-1]
if len(sys.argv) > 1 and lastarg[0] != '-':
    sys.argv[-1] = '-file'
    sys.argv.append(lastarg)

argparser = argparse.ArgumentParser()
argparser.add_argument('-d', action='store_true')
argparser.add_argument('-nrv', action='store_true')
argparser.add_argument('-file', type=str, default='')
args = argparser.parse_args()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM