Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -bash-3.2$ cat options.py
- #!/usr/bin/python
- import sys, getopt, string
- def help_message():
- print '''options.py -- uses getopt to recognize options
- Options: -h -- displays this help message
- -a -- expects an argument
- --file= -- expects an argument
- --view -- doesn't necessarily expect an argument
- --version -- displays Python version'''
- sys.exit(0)
- try:
- options, xarguments = getopt.getopt(sys.argv[1:],
- 'ha', ['file=', '--view', 'version'])
- except getopt.error:
- print 'Error: You tried to use an unknown option or the
- argument for an option that requires it was missing. Try
- `options.py -h\' for more information.'
- sys.exit(0)
- for a in options[:]:
- if a[0] == '-h':
- help_message()
- for a in options[:]:
- if a[0] == '-a' and a[1] != '':
- print a[0]+' = '+a[1]
- options.remove(a)
- break
- elif a[0] == '-a' and a[1] == '':
- print '-a expects an argument'
- sys.exit(0)
- for a in options[:]:
- if a[0] == '--file' and a[1] != '':
- print a[0]+' = '+a[1]
- options.remove(a)
- break
- elif a[0] == '--file' and a[1] == '':
- print '--file expects an argument'
- sys.exit(0)
- for a in options[:]:
- if a[0] == '--view' and a[1] != '':
- print a[0]+' = '+a[1]
- options.remove(a)
- break
- elif a[0] == '--view' and a[1] == '':
- print '--view doesn\'t necessarily expects an argument...'
- options.remove(a)
- sys.exit(0)
- for a in options[:]:
- if a[0] == '--version':
- print 'options version 0.0.001'
- sys.exit(0)
- for a in options[:]:
- if a[0] == '--python-version':
- print 'Python '+sys.version
- sys.exit(0)
- -bash-3.2$ ./options.py
- File "./options.py", line 15
- print 'Error: You tried to use an unknown option or the
- ^
- SyntaxError: EOL while scanning single-quoted string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement