I have argparse arguments setup in the following fashion:
parser = argparse.ArgumentParser(description='foobar')
parser.add_argument('url', metavar='URL')
parser.add_argument('-k', dest='kindle_type', default='kindle3')
parser.add_argument('-n', dest='gallery_name', default='Gallery')
parser.add_argument('-d', dest='dropbox_dir')
args = parser.parse_args()
print parser.parse_args(['imgur_url', '-k'])
However, when I run script.py -k kindledx http://url.com
, I get the following
error: gallery2kindle.py: error: argument -k: expected one argument
Wasn't an argument called when I used 'kindledx' after flag '-k'?
Wasn't an argument called when I used 'kindledx' after flag '-k'?
It was, and if you add a print args
after args = parser.parse_args()
, you can see the parsing works:
~/coding$ python ap.py -k kindledx http://url.com
Namespace(dropbox_dir=None, gallery_name='Gallery', kindle_type='kindledx', url='http://url.com')
but in this line
print parser.parse_args(['imgur_url', '-k'])
you don't pass an argument to k. Compare
print parser.parse_args(['imgur_url', '-k', 'kindledx'])
which produces
Namespace(dropbox_dir=None, gallery_name='Gallery', kindle_type='kindledx', url='imgur_url')
Since this is the first result of googling this error, I want to share that it can happen if you follow advice to compatible with Windows /
in Linux:
arg_parser = argparse.ArgumentParser(
prefix_chars='-+/', description='desc')
This causes -option /path/path
throws expected one argument
even though it looks like valid. You need quotes it to be -option '/path/path'
or remove the /
in the prefix_chars
above to fix it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With