getopt() not enforcing required arguments?

An option followed by a colon only means that it needs an argument. It doesn't mean that the option is enforced. You should write your own code to enforce the existence of options/arguments.


Just as a note, I found that argparse is simpler and more useful than getopt, and it support required arguments.

http://docs.python.org/2/howto/argparse.html#id1

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo", help="echo the string you use here")
args = parser.parse_args()

Command Line

$ python prog.py
usage: prog.py [-h] echo
prog.py: error: the following arguments are required: echo

Tags:

Python