Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. -bash-3.2$ cat options.py
  2. #!/usr/bin/python
  3. import sys, getopt, string
  4. def help_message():
  5.     print '''options.py -- uses getopt to recognize options
  6. Options: -h      -- displays this help message
  7.       -a      -- expects an argument
  8.       --file= -- expects an argument
  9.       --view  -- doesn't necessarily expect an argument
  10.       --version -- displays Python version'''
  11.     sys.exit(0)
  12. try:
  13.     options, xarguments = getopt.getopt(sys.argv[1:],
  14.     'ha', ['file=', '--view', 'version'])
  15. except getopt.error:
  16.     print 'Error: You tried to use an unknown option or the
  17.    argument for an option that requires it was missing. Try
  18.    `options.py -h\' for more information.'
  19.         sys.exit(0)
  20. for a in options[:]:
  21.     if a[0] == '-h':
  22.         help_message()
  23. for a in options[:]:
  24.     if a[0] == '-a' and a[1] != '':
  25.         print a[0]+' = '+a[1]
  26.         options.remove(a)
  27.         break
  28.     elif a[0] == '-a' and a[1] == '':
  29.         print '-a expects an argument'
  30.         sys.exit(0)
  31. for a in options[:]:
  32.     if a[0] == '--file' and a[1] != '':
  33.         print a[0]+' = '+a[1]
  34.         options.remove(a)
  35.         break
  36.     elif a[0] == '--file' and a[1] == '':
  37.         print '--file expects an argument'
  38.         sys.exit(0)
  39. for a in options[:]:
  40.     if a[0] == '--view' and a[1] != '':
  41.         print a[0]+' = '+a[1]
  42.         options.remove(a)
  43.         break
  44.     elif a[0] == '--view' and a[1] == '':
  45.         print '--view doesn\'t necessarily expects an argument...'
  46.         options.remove(a)
  47.         sys.exit(0)
  48. for a in options[:]:
  49.     if a[0] == '--version':
  50.         print 'options version 0.0.001'
  51.         sys.exit(0)
  52. for a in options[:]:
  53.     if a[0] == '--python-version':
  54.         print 'Python '+sys.version
  55.         sys.exit(0)
  56. -bash-3.2$ ./options.py
  57.   File "./options.py", line 15
  58.     print 'Error: You tried to use an unknown option or the
  59.                                                          ^
  60. SyntaxError: EOL while scanning single-quoted string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement