I'd like to use my program this way:
pythonScript -f filename
But if I forgot -f, I still want to get the filename.
So I want this "pythonScript filename" also work (->I can get the file name from args.fileName or from handling the exception)
I know this must be easy, I just can not find a way to do it. Anyone can help me out? Thanks
import argparse parser = argparse.ArgumentParser(prog='PROG') parser.add_argument('-f', action='store', dest='fileName', default = None, type=str, help='The file name') try: args = parser.parse_args() except: print "xxx"
-*is a flag, and option - it's not required. Plus, you never stated it should be required, quite the contrary, you even provided a default value.