learn/doc/python_argument_flag at main · gitLibs/learn · GitHub
Skip to content

Latest commit

 

History

History
 
 

Folders and files

README.md

back to contents

Python: argument, flag

↑ top




flag

#!/usr/bin/python -u
import sys
import getopt

usage = """
Usage:
    ./00_flag.py -i 8 -o hello -d 7 -f

"""

try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], "i:o:d:f")

except:
    print usage
    sys.exit(0)


if __name__ == "__main__":
    o = {}
    for opt, arg in opts:
        o[opt] = arg

    print '-f' in o

    from pprint import pprint
    pprint(o)

"""
./00_flag.py -i 8 -o hello -d 7 -f
True
{'-d': '7', '-f': '', '-i': '8', '-o': 'hello'}
"""

↑ top