Skip to content
Advertisement

Why is python getopt not parsing my options and instead think they are arguments?

I am attempting to use python’s getopt to parse some input arguments, but my options are not recognised. Why does the following happen? What am I doing wrong?

JavaScript

Advertisement

Answer

According to [Python2.Docs]: getopt.getopt(args, options[, long_options]) (emphasis is mine):

Note: Unlike GNU getopt(), after a non-option argument, all further arguments are considered also non-options. This is similar to the way non-GNU Unix systems work.

arg1 is such a non-option argument. Placing it at the end of the list (the 2nd call), would yield the expected output:

JavaScript

From the same page (what @tripleee also suggested):

Note that an equivalent command line interface could be produced with less code and more informative help and error messages by using the argparse module:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement