Python GetOpt

Python code posted by Naresh Khalasi
created at 01 Sep 20:54, updated at 01 Sep 20:54

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sys

def get_option_values(opts):
    optvals = {}
    for optname, optvalue in opts:
        if optname == '-u' and optvalue:
            optvals['username'] = optvalue
        elif optname == '-p' and optvalue:
            optvals['password'] = optvalue
        elif optname == '-f' and optvalue:
            optvals['filepath'] = optvalue
    if optvals.get('username',None) and optvals.get('password',None) and optvals.get('filepath',None):
        return optvals
    else:
        usage()

def usage():
    print '''Usage: %s -u <username> -p <password> -f <filepath>''' %(sys.argv[0])
    sys.exit()

if __name__ == '__main__':
    import getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'u:p:f:')
        optvals = get_option_values(opts)
   except getopt.GetoptError:
         usage()
843 Bytes in 3 ms with coderay