How can I specify that some command line arguments are mandatory in Python?

command-line, python

Solution

The simplest approach would be to do it yourself. I.e.

found_f = False
try:
    opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
except getopt.GetoptError, err:
    print str(err)
    usage()
    sys.exit(2)
for o, a in opts:
    if o == '-f':
      process_f()
      found_f = True
    elif ...
if not found_f:
    print "-f was not given"
    usage()
    sys.exit(2)

Problem

I'm writing a program in Python that accepts command line arguments. I am parsing them with `getopt` (though my choice of `getopt` is no Catholic marriage. I'm more than willing to use any other library). Is there any way to specify that certain arguments must be given, or do I have to manually make sure that all the arguments were given? Edit: I changed all instances of option to argument in response to public outcry. Let it not be said that I am not responsive to people who help me :-)

Original source