使用argparse管理多个以下位置参数

时间:2021-12-24 21:25:42

I'm using this lib for parsing arguments in python : https://docs.python.org/2/library/argparse.html

我正在使用这个lib来解析python中的参数:https://docs.python.org/2/library/argparse.html

So far I have this:

到目前为止我有这个:

prog arg1 [-s arg2 [arg2 ...]] [-m arg3 [arg3 ...]] 

And I want this:

我想要这个:

prog arg1 -s arg2 [arg2 ...] -m arg3 [arg3 ...]

Here is my python code:

这是我的python代码:

parser = argparse.ArgumentParser()
parser.add_argument('path', type=str,
                    help="path used for the generation of the rouge files")
parser.add_argument('-s', '--systems', type=str, nargs='+',
                    help="path to the systems generated summary files")
parser.add_argument('-m', '--models', type=str, nargs='+',
                    help="path to the reference summary files")
args = parser.parse_args()
print args

The problem is when you call the program without the optional arguments, it doesn't give an error (too few arguments). I want my optional arguments to be obligatory, but when you make the following call, the parser doesn't figure out which kind of args are involved...

问题是当你在没有可选参数的情况下调用程序时,它不会产生错误(参数太少)。我希望我的可选参数是强制性的,但是当你进行以下调用时,解析器并不知道涉及哪种args ......

For exemple with the following code:

例如,使用以下代码:

parser = argparse.ArgumentParser()
parser.add_argument('arg1', type=str, nargs='+')
parser.add_argument('arg2', type=str, nargs='+')
parser.add_argument('arg3', type=str, nargs='+')
args = parser.parse_args()

And the following call:

以下电话:

python test.py arg1 arg1 arg1 arg2 arg2 arg3 arg3

I got this:

我懂了:

Namespace(arg1=['arg1', 'arg1', 'arg1', 'arg2', 'arg2'], arg2=['arg3'], arg3=['arg3'])

For sure here is the format of this prog:

这肯定是这个编程的格式:

prog arg1 [arg1 ...] arg2 [arg2 ...] arg3

Thanks for the help :)

谢谢您的帮助 :)

3 个解决方案

#1


optionals can take a required=True parameter. That may be all you need.

optionals可以采用required = True参数。这可能就是你所需要的。

 p=argparse.ArgumentParser()
 p.add_argument('-m',nargs='+',required=True)
 p.add_argument('-n',nargs='+',required=True)
 p.print_usage()

producing

 usage: ipython3 [-h] -m M [M ...] -n N [N ...]

As to why:

至于为什么:

 Namespace(arg1=['arg1', 'arg1', 'arg1', 'arg2', 'arg2'], arg2=['arg3'], arg3=['arg3'])

you specified that each of arg1, arg2, arg3 required 1 or more strings. It split up the long list accordingly, giving arg2 and arg3 each one (which satisfies their requirement), and allocating the rest to arg1. If you are familiar with regex, this is equivalent

你指定arg1,arg2,arg3中的每一个都需要1个或更多字符串。它相应地拆分了长列表,给出了每个arg2和arg3(满足它们的要求),并将其余部分分配给arg1。如果您熟悉正则表达式,这是等效的

In [96]: re.match('(A+)(A+)(A+)','AAAAAAAAA').groups()
Out[96]: ('AAAAAAA', 'A', 'A')

(The parser can't read your mind and allocate all the 'arg2's to args2 just because the names look similar.:) )

(解析器无法读取你的想法并将所有'arg2'分配给args2只是因为名称看起来很相似。:))

So if you need to split lists of arguments in a specific way, then optionals (flags) is the way to go. And between nargs and required you have quite a bit of control over the numbers.

因此,如果您需要以特定方式拆分参数列表,那么可以使用选项(标志)。在麻烦和需要之间你需要对数字进行相当多的控制。

#2


What you want is not possible. Think about it: if you would implement your own argument parsing without argparse, how would you determine if a positional argument is the last one of a list of arg1 arguments, or the first of the arg2 arguments?

你想要的是不可能的。想一想:如果你在没有argparse的情况下实现自己的参数解析,你如何确定位置参数是arg1参数列表的最后一个,还是第一个arg2参数?

I think the solution you've got know (optional arguments) works fine and is even preferable.

我认为你已经知道的解决方案(可选参数)工作正常,甚至更好。

#3


I added require=True and it works, thanks to hpaulj

我添加了require = True并且它有效,感谢hpaulj

parser = argparse.ArgumentParser()
parser.add_argument('path', type=str,
                    help="path used for the generation of the rouge files")
parser.add_argument('-s', '--systems', type=str, nargs='+', required=True,
                    help="path to the systems generated summary files")
parser.add_argument('-m', '--models', type=str, nargs='+', required=True,
                    help="path to the reference summary files")
args = parser.parse_args()

python summary2rouge.py 
usage: summary2rouge.py [-h] -s SYSTEMS [SYSTEMS ...] -m MODELS [MODELS ...]
                    path
summary2rouge.py: error: too few arguments

#1


optionals can take a required=True parameter. That may be all you need.

optionals可以采用required = True参数。这可能就是你所需要的。

 p=argparse.ArgumentParser()
 p.add_argument('-m',nargs='+',required=True)
 p.add_argument('-n',nargs='+',required=True)
 p.print_usage()

producing

 usage: ipython3 [-h] -m M [M ...] -n N [N ...]

As to why:

至于为什么:

 Namespace(arg1=['arg1', 'arg1', 'arg1', 'arg2', 'arg2'], arg2=['arg3'], arg3=['arg3'])

you specified that each of arg1, arg2, arg3 required 1 or more strings. It split up the long list accordingly, giving arg2 and arg3 each one (which satisfies their requirement), and allocating the rest to arg1. If you are familiar with regex, this is equivalent

你指定arg1,arg2,arg3中的每一个都需要1个或更多字符串。它相应地拆分了长列表,给出了每个arg2和arg3(满足它们的要求),并将其余部分分配给arg1。如果您熟悉正则表达式,这是等效的

In [96]: re.match('(A+)(A+)(A+)','AAAAAAAAA').groups()
Out[96]: ('AAAAAAA', 'A', 'A')

(The parser can't read your mind and allocate all the 'arg2's to args2 just because the names look similar.:) )

(解析器无法读取你的想法并将所有'arg2'分配给args2只是因为名称看起来很相似。:))

So if you need to split lists of arguments in a specific way, then optionals (flags) is the way to go. And between nargs and required you have quite a bit of control over the numbers.

因此,如果您需要以特定方式拆分参数列表,那么可以使用选项(标志)。在麻烦和需要之间你需要对数字进行相当多的控制。

#2


What you want is not possible. Think about it: if you would implement your own argument parsing without argparse, how would you determine if a positional argument is the last one of a list of arg1 arguments, or the first of the arg2 arguments?

你想要的是不可能的。想一想:如果你在没有argparse的情况下实现自己的参数解析,你如何确定位置参数是arg1参数列表的最后一个,还是第一个arg2参数?

I think the solution you've got know (optional arguments) works fine and is even preferable.

我认为你已经知道的解决方案(可选参数)工作正常,甚至更好。

#3


I added require=True and it works, thanks to hpaulj

我添加了require = True并且它有效,感谢hpaulj

parser = argparse.ArgumentParser()
parser.add_argument('path', type=str,
                    help="path used for the generation of the rouge files")
parser.add_argument('-s', '--systems', type=str, nargs='+', required=True,
                    help="path to the systems generated summary files")
parser.add_argument('-m', '--models', type=str, nargs='+', required=True,
                    help="path to the reference summary files")
args = parser.parse_args()

python summary2rouge.py 
usage: summary2rouge.py [-h] -s SYSTEMS [SYSTEMS ...] -m MODELS [MODELS ...]
                    path
summary2rouge.py: error: too few arguments