I have this little problem with argparse :
#!/usr/bin/python2.6
#test.py
import argparse
parser = argparse.ArgumentParser(description="test")
parser.add_argument('c', nargs='*')
parser.add_argument('cj', nargs='*')
results = vars(parser.parse_args())
print results
Now in the command line if I type in : “test.py c 1”
it returns this
{‘cj’: [], ‘c’: [‘c’, ‘1’]}
but if I type in ” “test.py cj 1”
it returns this :
{‘cj’: [], ‘c’: [‘cj’, ‘1’]}
I am expecting the second example to return value in the ‘cj’ key, but it keeps on appears in the ‘c’ key.
what am I doing wrong ?
cheers,
Your issue is that the
*will match everything that comes after it. Since thecargument has the first*everything that is passed in will end up inc.If you want to store a single item in
cjand a single item incyou could do:If what you want is:
This is because the
+matches a single argument.