问题
I am trying to use argparse module to parse the arguments in command line. here is the sample code
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('bar')
parser.add_argument('-foo')
args=parser.parse_args()
print args
running this
python argparsing.py "hello"
Namespace(bar='hello', foo=None)
however, this does not work
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('bar','--foo')
#parser.add_argument('-foo')
args=parser.parse_args()
print args
running this gives error
python argparsing.py "hello"
Traceback (most recent call last):
File "argparsing.py", line 3, in <module>
parser.add_argument('bar','-foo')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1267, in add_argument
kwargs = self._get_optional_kwargs(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1397, in _get_optional_kwargs
raise ValueError(msg % tup)
ValueError: invalid option string 'bar': must start with a character '-'
The only difference between the two is I am adding argument separately (in two lines) in the first code but adding them in the same line as one statement in the failed code. can someone help me understand this better?
回答1:
You're not doing the same thing. Look at the docs:
name or flags - Either a name or a list of option strings, e.g.
foo
or-f, --foo
.
You can't specify a list of positional arguments, just a single positional argument, or a list of option (flag) arguments. So, when you try to give it 'bar', '--foo'
, it tries to parse 'bar'
as an option, and it can't, because it doesn't start with a -
.
And, as the next section explains, when you give it a list of options, you're giving it a list of alternative names for the same flag, not a bunch of separate flags. So, you don't want to do what you're trying to do in the first place.
回答2:
add_argument
is meant to add a single argument. 'bar'
is one positional argument; '--foo'
is one optional argument. You can't make an argument that is both positional and optional, hence the error.
If you want to define an argument, but name the option differently, use dest
:
# accept an option --foo X, and store X into args.bar
parser.add_argument('--foo', dest='bar')
Note that you can define multiple aliases for a single optional argument, e.g.
parser.add_argument('-f', '--foo', '--frobnicate')
回答3:
So you're giving argparse
two totally different ways of defining an argument. So it's throwing an error because it can't understand your input
One method of defining an argument is using no dashes like you had first
parser.add_argument("bar") # This is a required argument
Another is to use dashes
parser.add_argument("-f", "--foo")
--foo
is just another way of specifying the same argument as -f
. It is not a required argument like ones with no dashes (bar
) unless you want it to be a required argument
来源:https://stackoverflow.com/questions/19126399/why-args-add-argument-works-when-given-in-two-separate-statements-but-not-one-in