Argparse: one argument expected

I have argparse argument arguments as follows:

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

The argument was not called when I used "kindledx" after the "-k" flag?

+6
source share
2 answers

The argument was not called when I used "kindledx" after the "-k" flag?

That was, and if you add print args after args = parser.parse_args() , you will see that 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 on this line

 print parser.parse_args(['imgur_url', '-k']) 

you are not passing argument 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') 
+7
source

Since this is the first search result for this error, I want to share that this can happen if you follow the Windows / Linux compatibility tips:

 arg_parser = argparse.ArgumentParser( prefix_chars='-+/', description='desc') 

This causes the -option /path/path throws expected one argument , even if they look valid. You need to specify -option '/path/path' in quotation marks or remove / in prefix_chars above to fix this.

0
source

All Articles