getopt.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: iso-8859-1 -*-
  2. """Parser for command line options.
  3. This module helps scripts to parse the command line arguments in
  4. sys.argv. It supports the same conventions as the Unix getopt()
  5. function (including the special meanings of arguments of the form `-'
  6. and `--'). Long options similar to those supported by GNU software
  7. may be used as well via an optional third argument. This module
  8. provides two functions and an exception:
  9. getopt() -- Parse command line options
  10. gnu_getopt() -- Like getopt(), but allow option and non-option arguments
  11. to be intermixed.
  12. GetoptError -- exception (class) raised with 'opt' attribute, which is the
  13. option involved with the exception.
  14. """
  15. # Long option support added by Lars Wirzenius <[email protected]>.
  16. #
  17. # Gerrit Holl <[email protected]> moved the string-based exceptions
  18. # to class-based exceptions.
  19. #
  20. # Peter Åstrand <[email protected]> added gnu_getopt().
  21. #
  22. # TODO for gnu_getopt():
  23. #
  24. # - GNU getopt_long_only mechanism
  25. # - allow the caller to specify ordering
  26. # - RETURN_IN_ORDER option
  27. # - GNU extension with '-' as first character of option string
  28. # - optional arguments, specified by double colons
  29. # - a option string with a W followed by semicolon should
  30. # treat "-W foo" as "--foo"
  31. __all__ = ["GetoptError","error","getopt","gnu_getopt"]
  32. import os
  33. class GetoptError(Exception):
  34. opt = ''
  35. msg = ''
  36. def __init__(self, msg, opt=''):
  37. self.msg = msg
  38. self.opt = opt
  39. Exception.__init__(self, msg, opt)
  40. def __str__(self):
  41. return self.msg
  42. error = GetoptError # backward compatibility
  43. def getopt(args, shortopts, longopts = []):
  44. """getopt(args, options[, long_options]) -> opts, args
  45. Parses command line options and parameter list. args is the
  46. argument list to be parsed, without the leading reference to the
  47. running program. Typically, this means "sys.argv[1:]". shortopts
  48. is the string of option letters that the script wants to
  49. recognize, with options that require an argument followed by a
  50. colon (i.e., the same format that Unix getopt() uses). If
  51. specified, longopts is a list of strings with the names of the
  52. long options which should be supported. The leading '--'
  53. characters should not be included in the option name. Options
  54. which require an argument should be followed by an equal sign
  55. ('=').
  56. The return value consists of two elements: the first is a list of
  57. (option, value) pairs; the second is the list of program arguments
  58. left after the option list was stripped (this is a trailing slice
  59. of the first argument). Each option-and-value pair returned has
  60. the option as its first element, prefixed with a hyphen (e.g.,
  61. '-x'), and the option argument as its second element, or an empty
  62. string if the option has no argument. The options occur in the
  63. list in the same order in which they were found, thus allowing
  64. multiple occurrences. Long and short options may be mixed.
  65. """
  66. opts = []
  67. if type(longopts) == type(""):
  68. longopts = [longopts]
  69. else:
  70. longopts = list(longopts)
  71. while args and args[0].startswith('-') and args[0] != '-':
  72. if args[0] == '--':
  73. args = args[1:]
  74. break
  75. if args[0].startswith('--'):
  76. opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
  77. else:
  78. opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
  79. return opts, args
  80. def gnu_getopt(args, shortopts, longopts = []):
  81. """getopt(args, options[, long_options]) -> opts, args
  82. This function works like getopt(), except that GNU style scanning
  83. mode is used by default. This means that option and non-option
  84. arguments may be intermixed. The getopt() function stops
  85. processing options as soon as a non-option argument is
  86. encountered.
  87. If the first character of the option string is `+', or if the
  88. environment variable POSIXLY_CORRECT is set, then option
  89. processing stops as soon as a non-option argument is encountered.
  90. """
  91. opts = []
  92. prog_args = []
  93. if isinstance(longopts, str):
  94. longopts = [longopts]
  95. else:
  96. longopts = list(longopts)
  97. # Allow options after non-option arguments?
  98. if shortopts.startswith('+'):
  99. shortopts = shortopts[1:]
  100. all_options_first = True
  101. elif os.environ.get("POSIXLY_CORRECT"):
  102. all_options_first = True
  103. else:
  104. all_options_first = False
  105. while args:
  106. if args[0] == '--':
  107. prog_args += args[1:]
  108. break
  109. if args[0][:2] == '--':
  110. opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
  111. elif args[0][:1] == '-':
  112. opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
  113. else:
  114. if all_options_first:
  115. prog_args += args
  116. break
  117. else:
  118. prog_args.append(args[0])
  119. args = args[1:]
  120. return opts, prog_args
  121. def do_longs(opts, opt, longopts, args):
  122. try:
  123. i = opt.index('=')
  124. except ValueError:
  125. optarg = None
  126. else:
  127. opt, optarg = opt[:i], opt[i+1:]
  128. has_arg, opt = long_has_args(opt, longopts)
  129. if has_arg:
  130. if optarg is None:
  131. if not args:
  132. raise GetoptError('option --%s requires argument' % opt, opt)
  133. optarg, args = args[0], args[1:]
  134. elif optarg:
  135. raise GetoptError('option --%s must not have an argument' % opt, opt)
  136. opts.append(('--' + opt, optarg or ''))
  137. return opts, args
  138. # Return:
  139. # has_arg?
  140. # full option name
  141. def long_has_args(opt, longopts):
  142. possibilities = [o for o in longopts if o.startswith(opt)]
  143. if not possibilities:
  144. raise GetoptError('option --%s not recognized' % opt, opt)
  145. # Is there an exact match?
  146. if opt in possibilities:
  147. return False, opt
  148. elif opt + '=' in possibilities:
  149. return True, opt
  150. # No exact match, so better be unique.
  151. if len(possibilities) > 1:
  152. # XXX since possibilities contains all valid continuations, might be
  153. # nice to work them into the error msg
  154. raise GetoptError('option --%s not a unique prefix' % opt, opt)
  155. assert len(possibilities) == 1
  156. unique_match = possibilities[0]
  157. has_arg = unique_match.endswith('=')
  158. if has_arg:
  159. unique_match = unique_match[:-1]
  160. return has_arg, unique_match
  161. def do_shorts(opts, optstring, shortopts, args):
  162. while optstring != '':
  163. opt, optstring = optstring[0], optstring[1:]
  164. if short_has_arg(opt, shortopts):
  165. if optstring == '':
  166. if not args:
  167. raise GetoptError('option -%s requires argument' % opt,
  168. opt)
  169. optstring, args = args[0], args[1:]
  170. optarg, optstring = optstring, ''
  171. else:
  172. optarg = ''
  173. opts.append(('-' + opt, optarg))
  174. return opts, args
  175. def short_has_arg(opt, shortopts):
  176. for i in range(len(shortopts)):
  177. if opt == shortopts[i] != ':':
  178. return shortopts.startswith(':', i+1)
  179. raise GetoptError('option -%s not recognized' % opt, opt)
  180. if __name__ == '__main__':
  181. import sys
  182. print getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])