The argparse module will give you a lot more flexibility in playing with command line arguments.
import argparse parser = argparse.ArgumentParser(prog='uppercase') parser.add_argument('-f','--filename', help='Any text file will do.') # filename arg parser.add_argument('-u','--uppercase', action='store_true', help='If set, all letters become uppercase.') # boolean arg args = parser.parse_args() if args.filename: # if a filename is supplied... print 'reading file...' f = open(args.filename).read() if args.uppercase: # and if boolean argument is given... print f.upper() # do your thing else: print f # or do nothing else: parser.print_help() # or print help
So, when you start with no arguments, you get:
/home/myuser$ python test.py usage: uppercase [-h] [-f FILENAME] [-u] optional arguments: -h, --help show this help message and exit -f FILENAME, --filename FILENAME Any text file will do. -u, --uppercase If set, all letters become uppercase.
dmvianna
source share