getopt.py 7.1 KB

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