ds_practicioner
ds_practicioner

Reputation: 733

Python enumeration dropping the last element of a list

I am working off this article here trying to parse some command line arguments but the script I built keeps dropping the last argument.

To keep it simple I reproduced the problem like so:

import getopt

argv = ["-c", "config", "-o", "hello", "-e", "fu bar", "-q", "this is a query"]
opts, args = getopt.getopt(argv, "c:o:e:q", ["cfile=", "ofile=", "entry=", "query="])

for opt, arg in opts:
    print(opt, arg)

Here is what I get for output:

-c config
-o hello
-e fu bar
-q

Where am I going wrong?

Upvotes: 2

Views: 132

Answers (1)

Selcuk
Selcuk

Reputation: 59323

The colon (:) is not a separator, it needs to follow each argument as stated in the docs:

shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':'; i.e., the same format that Unix getopt() uses).

Therefore you should change "c:o:e:q" to "c:o:e:q:"

The tutorial you linked to uses it the same way too.

Upvotes: 6

Related Questions