python argh/argparse:如何将列表作为命令行参数传递?

时间:2022-12-10 23:15:09

I'm trying to pass a list of arguments to a python script using the argh library. Something that can take inputs like these:

我正在尝试使用argh库将参数列表传递给python脚本。可以采用这样的输入:

./my_script.py my-func --argA blah --argB 1 2 3 4
./my_script.py my-func --argA blah --argB 1
./my_script.py my-func --argA blah --argB 

My internal code looks like this:

我的内部代码是这样的:

import argh

@argh.arg('--argA', default="bleh", help='My first arg')
@argh.arg('--argB', default=[], help='A list-type arg--except it\'s not!')
def my_func(args):
    "A function that does something"

    print args.argA
    print args.argB

    for b in args.argB:
        print int(b)*int(b)  #Print the square of each number in the list
    print sum([int(b) for b in args.argB])  #Print the sum of the list

p = argh.ArghParser()
p.add_commands([my_func])
p.dispatch()

And here's how it behaves:

这是它的行为:

$ python temp.py my-func --argA blooh --argB 1
blooh
['1']
1
1

$ python temp.py my-func --argA blooh --argB 10
blooh
['1', '0']
1
0
1

$ python temp.py my-func --argA blooh --argB 1 2 3
usage: temp.py [-h] {my-func} ...
temp.py: error: unrecognized arguments: 2 3

The problem seems pretty straightforward: argh is only accepting the first argument, and treating it as a string. How do I make it "expect" a list of integers instead?

问题似乎很简单:argh只接受第一个参数,并将其作为字符串处理。如何让它“expect”一个整数列表呢?

I see how this is done in optparse, but what about the (not-deprecated) argparse? Or using argh's much nicer decorated syntax? These seem much more pythonic.

我看到了在optparse中是如何完成的,但是(不弃用的)argparse又是怎样的呢?或者使用argh更好的修饰语法?这些看起来更像蟒蛇。

2 个解决方案

#1


77  

With argparse, you just use type=int

使用argparse,您只需使用type=int。

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-a', '--arg', nargs='+', type=int)
print parser.parse_args()

Example output:

示例输出:

$ python test.py -a 1 2 3
Namespace(arg=[1, 2, 3])

Edit: I'm not familiar with argh, but it seems to be just a wrapper around argparse and this worked for me:

编辑:我对argh不熟悉,但它似乎只是一个围绕着argparse的包装器,这对我很有用:

import argh

@argh.arg('-a', '--arg', nargs='+', type=int)
def main(args):
    print args

parser = argh.ArghParser()
parser.add_commands([main])
parser.dispatch()

Example output:

示例输出:

$ python test.py main -a 1 2 3
Namespace(arg=[1, 2, 3], function=<function main at 0x.......>)

#2


1  

I order to have access to each parameter value, the following code may be helpful.

我要访问每个参数值,下面的代码可能会有所帮助。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--arg', nargs='+', type=int)
args = vars(parser.parse_args())

print "first parameter:" + str(args["arg"][0])
print "second parameter:" + str(args["arg"][1])
print "third parameter:" + str(args["arg"][2])

#1


77  

With argparse, you just use type=int

使用argparse,您只需使用type=int。

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-a', '--arg', nargs='+', type=int)
print parser.parse_args()

Example output:

示例输出:

$ python test.py -a 1 2 3
Namespace(arg=[1, 2, 3])

Edit: I'm not familiar with argh, but it seems to be just a wrapper around argparse and this worked for me:

编辑:我对argh不熟悉,但它似乎只是一个围绕着argparse的包装器,这对我很有用:

import argh

@argh.arg('-a', '--arg', nargs='+', type=int)
def main(args):
    print args

parser = argh.ArghParser()
parser.add_commands([main])
parser.dispatch()

Example output:

示例输出:

$ python test.py main -a 1 2 3
Namespace(arg=[1, 2, 3], function=<function main at 0x.......>)

#2


1  

I order to have access to each parameter value, the following code may be helpful.

我要访问每个参数值,下面的代码可能会有所帮助。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--arg', nargs='+', type=int)
args = vars(parser.parse_args())

print "first parameter:" + str(args["arg"][0])
print "second parameter:" + str(args["arg"][1])
print "third parameter:" + str(args["arg"][2])