73

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

./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?

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.

Braiam
  • 1
  • 11
  • 47
  • 78
Abe
  • 22,738
  • 26
  • 82
  • 111

2 Answers2

101

With argparse, you just use 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:

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.......>)
jcollado
  • 39,419
  • 8
  • 102
  • 133
  • 1
    Awesome! I tweaked the code to include the line: @argh.arg('--argB', default=[], help='A list-type arg--except it\'s not!', nargs='+', type=int) and argh liked it. Thanks for the help! – Abe Feb 22 '12 at 15:58
  • Yep, this is the correct way. The recent versions of Argh also support the "natural", more pythonic notation: `func(argA, *argB)` (though `*args` maps to `nargs='*'` instead of `nargs='+'`). – Andy Mikhailenko Jan 15 '13 at 17:02
  • @jcollado : I see that when i use nargs= , i cannot do -a=1 2 3 . What I mean is that use '=' after the argument metavar. Any solution to let me use = for every arg. For ex in your example i wanna do , python test.py main -a=1 2 3 – script_kiddie Sep 21 '13 at 22:58
  • @Andy Mikhaylenko The above question. – script_kiddie Sep 21 '13 at 22:59
  • @AndyMikhaylenko I don't know of any straightforward way to do that. One alternative would be something like: `parser.add_argument('-a', '--arg', type=lambda s: [int(n) for n in s.split()])` and call the script using quotes: `python test.py --arg="1 2 3"` – jcollado Sep 22 '13 at 08:22
  • @script_kiddie @jcollado the problem is in the `=` character. `-a 1 2` means "a = [1, 2]" while `-a=1 2` means "a = 1; some-positional-argument = 2". – Andy Mikhailenko Oct 29 '13 at 22:32
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])
Luis Jose
  • 664
  • 6
  • 8