getopt.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Åstrand <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. try:
  33. from gettext import gettext as _
  34. except ImportError:
  35. # Bootstrapping Python: gettext's dependencies not built yet
  36. def _(s): return s
  37. class GetoptError(Exception):
  38. opt = ''
  39. msg = ''
  40. def __init__(self, msg, opt=''):
  41. self.msg = msg
  42. self.opt = opt
  43. Exception.__init__(self, msg, opt)
  44. def __str__(self):
  45. return self.msg
  46. error = GetoptError # backward compatibility
  47. def getopt(args, shortopts, longopts = []):
  48. """getopt(args, options[, long_options]) -> opts, args
  49. Parses command line options and parameter list. args is the
  50. argument list to be parsed, without the leading reference to the
  51. running program. Typically, this means "sys.argv[1:]". shortopts
  52. is the string of option letters that the script wants to
  53. recognize, with options that require an argument followed by a
  54. colon (i.e., the same format that Unix getopt() uses). If
  55. specified, longopts is a list of strings with the names of the
  56. long options which should be supported. The leading '--'
  57. characters should not be included in the option name. Options
  58. which require an argument should be followed by an equal sign
  59. ('=').
  60. The return value consists of two elements: the first is a list of
  61. (option, value) pairs; the second is the list of program arguments
  62. left after the option list was stripped (this is a trailing slice
  63. of the first argument). Each option-and-value pair returned has
  64. the option as its first element, prefixed with a hyphen (e.g.,
  65. '-x'), and the option argument as its second element, or an empty
  66. string if the option has no argument. The options occur in the
  67. list in the same order in which they were found, thus allowing
  68. multiple occurrences. Long and short options may be mixed.
  69. """
  70. opts = []
  71. if type(longopts) == type(""):
  72. longopts = [longopts]
  73. else:
  74. longopts = list(longopts)
  75. while args and args[0].startswith('-') and args[0] != '-':
  76. if args[0] == '--':
  77. args = args[1:]
  78. break
  79. if args[0].startswith('--'):
  80. opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
  81. else:
  82. opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
  83. return opts, args
  84. def gnu_getopt(args, shortopts, longopts = []):
  85. """getopt(args, options[, long_options]) -> opts, args
  86. This function works like getopt(), except that GNU style scanning
  87. mode is used by default. This means that option and non-option
  88. arguments may be intermixed. The getopt() function stops
  89. processing options as soon as a non-option argument is
  90. encountered.
  91. If the first character of the option string is `+', or if the
  92. environment variable POSIXLY_CORRECT is set, then option
  93. processing stops as soon as a non-option argument is encountered.
  94. """
  95. opts = []
  96. prog_args = []
  97. if isinstance(longopts, str):
  98. longopts = [longopts]
  99. else:
  100. longopts = list(longopts)
  101. # Allow options after non-option arguments?
  102. if shortopts.startswith('+'):
  103. shortopts = shortopts[1:]
  104. all_options_first = True
  105. elif os.environ.get("POSIXLY_CORRECT"):
  106. all_options_first = True
  107. else:
  108. all_options_first = False
  109. while args:
  110. if args[0] == '--':
  111. prog_args += args[1:]
  112. break
  113. if args[0][:2] == '--':
  114. opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
  115. elif args[0][:1] == '-' and args[0] != '-':
  116. opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
  117. else:
  118. if all_options_first:
  119. prog_args += args
  120. break
  121. else:
  122. prog_args.append(args[0])
  123. args = args[1:]
  124. return opts, prog_args
  125. def do_longs(opts, opt, longopts, args):
  126. try:
  127. i = opt.index('=')
  128. except ValueError:
  129. optarg = None
  130. else:
  131. opt, optarg = opt[:i], opt[i+1:]
  132. has_arg, opt = long_has_args(opt, longopts)
  133. if has_arg:
  134. if optarg is None:
  135. if not args:
  136. raise GetoptError(_('option --%s requires argument') % opt, opt)
  137. optarg, args = args[0], args[1:]
  138. elif optarg is not None:
  139. raise GetoptError(_('option --%s must not have an argument') % opt, opt)
  140. opts.append(('--' + opt, optarg or ''))
  141. return opts, args
  142. # Return:
  143. # has_arg?
  144. # full option name
  145. def long_has_args(opt, longopts):
  146. possibilities = [o for o in longopts if o.startswith(opt)]
  147. if not possibilities:
  148. raise GetoptError(_('option --%s not recognized') % opt, opt)
  149. # Is there an exact match?
  150. if opt in possibilities:
  151. return False, opt
  152. elif opt + '=' in possibilities:
  153. return True, opt
  154. # No exact match, so better be unique.
  155. if len(possibilities) > 1:
  156. # XXX since possibilities contains all valid continuations, might be
  157. # nice to work them into the error msg
  158. raise GetoptError(_('option --%s not a unique prefix') % opt, opt)
  159. assert len(possibilities) == 1
  160. unique_match = possibilities[0]
  161. has_arg = unique_match.endswith('=')
  162. if has_arg:
  163. unique_match = unique_match[:-1]
  164. return has_arg, unique_match
  165. def do_shorts(opts, optstring, shortopts, args):
  166. while optstring != '':
  167. opt, optstring = optstring[0], optstring[1:]
  168. if short_has_arg(opt, shortopts):
  169. if optstring == '':
  170. if not args:
  171. raise GetoptError(_('option -%s requires argument') % opt,
  172. opt)
  173. optstring, args = args[0], args[1:]
  174. optarg, optstring = optstring, ''
  175. else:
  176. optarg = ''
  177. opts.append(('-' + opt, optarg))
  178. return opts, args
  179. def short_has_arg(opt, shortopts):
  180. for i in range(len(shortopts)):
  181. if opt == shortopts[i] != ':':
  182. return shortopts.startswith(':', i+1)
  183. raise GetoptError(_('option -%s not recognized') % opt, opt)
  184. if __name__ == '__main__':
  185. import sys
  186. print(getopt(sys.argv[1:], "a:b", ["alpha=", "beta"]))