parser.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. # -*- coding: utf-8 -*-
  2. """
  3. This module started out as largely a copy paste from the stdlib's
  4. optparse module with the features removed that we do not need from
  5. optparse because we implement them in Click on a higher level (for
  6. instance type handling, help formatting and a lot more).
  7. The plan is to remove more and more from here over time.
  8. The reason this is a different module and not optparse from the stdlib
  9. is that there are differences in 2.x and 3.x about the error messages
  10. generated and optparse in the stdlib uses gettext for no good reason
  11. and might cause us issues.
  12. Click uses parts of optparse written by Gregory P. Ward and maintained
  13. by the Python Software Foundation. This is limited to code in parser.py.
  14. Copyright 2001-2006 Gregory P. Ward. All rights reserved.
  15. Copyright 2002-2006 Python Software Foundation. All rights reserved.
  16. """
  17. import re
  18. from collections import deque
  19. from .exceptions import BadArgumentUsage
  20. from .exceptions import BadOptionUsage
  21. from .exceptions import NoSuchOption
  22. from .exceptions import UsageError
  23. def _unpack_args(args, nargs_spec):
  24. """Given an iterable of arguments and an iterable of nargs specifications,
  25. it returns a tuple with all the unpacked arguments at the first index
  26. and all remaining arguments as the second.
  27. The nargs specification is the number of arguments that should be consumed
  28. or `-1` to indicate that this position should eat up all the remainders.
  29. Missing items are filled with `None`.
  30. """
  31. args = deque(args)
  32. nargs_spec = deque(nargs_spec)
  33. rv = []
  34. spos = None
  35. def _fetch(c):
  36. try:
  37. if spos is None:
  38. return c.popleft()
  39. else:
  40. return c.pop()
  41. except IndexError:
  42. return None
  43. while nargs_spec:
  44. nargs = _fetch(nargs_spec)
  45. if nargs == 1:
  46. rv.append(_fetch(args))
  47. elif nargs > 1:
  48. x = [_fetch(args) for _ in range(nargs)]
  49. # If we're reversed, we're pulling in the arguments in reverse,
  50. # so we need to turn them around.
  51. if spos is not None:
  52. x.reverse()
  53. rv.append(tuple(x))
  54. elif nargs < 0:
  55. if spos is not None:
  56. raise TypeError("Cannot have two nargs < 0")
  57. spos = len(rv)
  58. rv.append(None)
  59. # spos is the position of the wildcard (star). If it's not `None`,
  60. # we fill it with the remainder.
  61. if spos is not None:
  62. rv[spos] = tuple(args)
  63. args = []
  64. rv[spos + 1 :] = reversed(rv[spos + 1 :])
  65. return tuple(rv), list(args)
  66. def _error_opt_args(nargs, opt):
  67. if nargs == 1:
  68. raise BadOptionUsage(opt, "{} option requires an argument".format(opt))
  69. raise BadOptionUsage(opt, "{} option requires {} arguments".format(opt, nargs))
  70. def split_opt(opt):
  71. first = opt[:1]
  72. if first.isalnum():
  73. return "", opt
  74. if opt[1:2] == first:
  75. return opt[:2], opt[2:]
  76. return first, opt[1:]
  77. def normalize_opt(opt, ctx):
  78. if ctx is None or ctx.token_normalize_func is None:
  79. return opt
  80. prefix, opt = split_opt(opt)
  81. return prefix + ctx.token_normalize_func(opt)
  82. def split_arg_string(string):
  83. """Given an argument string this attempts to split it into small parts."""
  84. rv = []
  85. for match in re.finditer(
  86. r"('([^'\\]*(?:\\.[^'\\]*)*)'|\"([^\"\\]*(?:\\.[^\"\\]*)*)\"|\S+)\s*",
  87. string,
  88. re.S,
  89. ):
  90. arg = match.group().strip()
  91. if arg[:1] == arg[-1:] and arg[:1] in "\"'":
  92. arg = arg[1:-1].encode("ascii", "backslashreplace").decode("unicode-escape")
  93. try:
  94. arg = type(string)(arg)
  95. except UnicodeError:
  96. pass
  97. rv.append(arg)
  98. return rv
  99. class Option(object):
  100. def __init__(self, opts, dest, action=None, nargs=1, const=None, obj=None):
  101. self._short_opts = []
  102. self._long_opts = []
  103. self.prefixes = set()
  104. for opt in opts:
  105. prefix, value = split_opt(opt)
  106. if not prefix:
  107. raise ValueError("Invalid start character for option ({})".format(opt))
  108. self.prefixes.add(prefix[0])
  109. if len(prefix) == 1 and len(value) == 1:
  110. self._short_opts.append(opt)
  111. else:
  112. self._long_opts.append(opt)
  113. self.prefixes.add(prefix)
  114. if action is None:
  115. action = "store"
  116. self.dest = dest
  117. self.action = action
  118. self.nargs = nargs
  119. self.const = const
  120. self.obj = obj
  121. @property
  122. def takes_value(self):
  123. return self.action in ("store", "append")
  124. def process(self, value, state):
  125. if self.action == "store":
  126. state.opts[self.dest] = value
  127. elif self.action == "store_const":
  128. state.opts[self.dest] = self.const
  129. elif self.action == "append":
  130. state.opts.setdefault(self.dest, []).append(value)
  131. elif self.action == "append_const":
  132. state.opts.setdefault(self.dest, []).append(self.const)
  133. elif self.action == "count":
  134. state.opts[self.dest] = state.opts.get(self.dest, 0) + 1
  135. else:
  136. raise ValueError("unknown action '{}'".format(self.action))
  137. state.order.append(self.obj)
  138. class Argument(object):
  139. def __init__(self, dest, nargs=1, obj=None):
  140. self.dest = dest
  141. self.nargs = nargs
  142. self.obj = obj
  143. def process(self, value, state):
  144. if self.nargs > 1:
  145. holes = sum(1 for x in value if x is None)
  146. if holes == len(value):
  147. value = None
  148. elif holes != 0:
  149. raise BadArgumentUsage(
  150. "argument {} takes {} values".format(self.dest, self.nargs)
  151. )
  152. state.opts[self.dest] = value
  153. state.order.append(self.obj)
  154. class ParsingState(object):
  155. def __init__(self, rargs):
  156. self.opts = {}
  157. self.largs = []
  158. self.rargs = rargs
  159. self.order = []
  160. class OptionParser(object):
  161. """The option parser is an internal class that is ultimately used to
  162. parse options and arguments. It's modelled after optparse and brings
  163. a similar but vastly simplified API. It should generally not be used
  164. directly as the high level Click classes wrap it for you.
  165. It's not nearly as extensible as optparse or argparse as it does not
  166. implement features that are implemented on a higher level (such as
  167. types or defaults).
  168. :param ctx: optionally the :class:`~click.Context` where this parser
  169. should go with.
  170. """
  171. def __init__(self, ctx=None):
  172. #: The :class:`~click.Context` for this parser. This might be
  173. #: `None` for some advanced use cases.
  174. self.ctx = ctx
  175. #: This controls how the parser deals with interspersed arguments.
  176. #: If this is set to `False`, the parser will stop on the first
  177. #: non-option. Click uses this to implement nested subcommands
  178. #: safely.
  179. self.allow_interspersed_args = True
  180. #: This tells the parser how to deal with unknown options. By
  181. #: default it will error out (which is sensible), but there is a
  182. #: second mode where it will ignore it and continue processing
  183. #: after shifting all the unknown options into the resulting args.
  184. self.ignore_unknown_options = False
  185. if ctx is not None:
  186. self.allow_interspersed_args = ctx.allow_interspersed_args
  187. self.ignore_unknown_options = ctx.ignore_unknown_options
  188. self._short_opt = {}
  189. self._long_opt = {}
  190. self._opt_prefixes = {"-", "--"}
  191. self._args = []
  192. def add_option(self, opts, dest, action=None, nargs=1, const=None, obj=None):
  193. """Adds a new option named `dest` to the parser. The destination
  194. is not inferred (unlike with optparse) and needs to be explicitly
  195. provided. Action can be any of ``store``, ``store_const``,
  196. ``append``, ``appnd_const`` or ``count``.
  197. The `obj` can be used to identify the option in the order list
  198. that is returned from the parser.
  199. """
  200. if obj is None:
  201. obj = dest
  202. opts = [normalize_opt(opt, self.ctx) for opt in opts]
  203. option = Option(opts, dest, action=action, nargs=nargs, const=const, obj=obj)
  204. self._opt_prefixes.update(option.prefixes)
  205. for opt in option._short_opts:
  206. self._short_opt[opt] = option
  207. for opt in option._long_opts:
  208. self._long_opt[opt] = option
  209. def add_argument(self, dest, nargs=1, obj=None):
  210. """Adds a positional argument named `dest` to the parser.
  211. The `obj` can be used to identify the option in the order list
  212. that is returned from the parser.
  213. """
  214. if obj is None:
  215. obj = dest
  216. self._args.append(Argument(dest=dest, nargs=nargs, obj=obj))
  217. def parse_args(self, args):
  218. """Parses positional arguments and returns ``(values, args, order)``
  219. for the parsed options and arguments as well as the leftover
  220. arguments if there are any. The order is a list of objects as they
  221. appear on the command line. If arguments appear multiple times they
  222. will be memorized multiple times as well.
  223. """
  224. state = ParsingState(args)
  225. try:
  226. self._process_args_for_options(state)
  227. self._process_args_for_args(state)
  228. except UsageError:
  229. if self.ctx is None or not self.ctx.resilient_parsing:
  230. raise
  231. return state.opts, state.largs, state.order
  232. def _process_args_for_args(self, state):
  233. pargs, args = _unpack_args(
  234. state.largs + state.rargs, [x.nargs for x in self._args]
  235. )
  236. for idx, arg in enumerate(self._args):
  237. arg.process(pargs[idx], state)
  238. state.largs = args
  239. state.rargs = []
  240. def _process_args_for_options(self, state):
  241. while state.rargs:
  242. arg = state.rargs.pop(0)
  243. arglen = len(arg)
  244. # Double dashes always handled explicitly regardless of what
  245. # prefixes are valid.
  246. if arg == "--":
  247. return
  248. elif arg[:1] in self._opt_prefixes and arglen > 1:
  249. self._process_opts(arg, state)
  250. elif self.allow_interspersed_args:
  251. state.largs.append(arg)
  252. else:
  253. state.rargs.insert(0, arg)
  254. return
  255. # Say this is the original argument list:
  256. # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]
  257. # ^
  258. # (we are about to process arg(i)).
  259. #
  260. # Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of
  261. # [arg0, ..., arg(i-1)] (any options and their arguments will have
  262. # been removed from largs).
  263. #
  264. # The while loop will usually consume 1 or more arguments per pass.
  265. # If it consumes 1 (eg. arg is an option that takes no arguments),
  266. # then after _process_arg() is done the situation is:
  267. #
  268. # largs = subset of [arg0, ..., arg(i)]
  269. # rargs = [arg(i+1), ..., arg(N-1)]
  270. #
  271. # If allow_interspersed_args is false, largs will always be
  272. # *empty* -- still a subset of [arg0, ..., arg(i-1)], but
  273. # not a very interesting subset!
  274. def _match_long_opt(self, opt, explicit_value, state):
  275. if opt not in self._long_opt:
  276. possibilities = [word for word in self._long_opt if word.startswith(opt)]
  277. raise NoSuchOption(opt, possibilities=possibilities, ctx=self.ctx)
  278. option = self._long_opt[opt]
  279. if option.takes_value:
  280. # At this point it's safe to modify rargs by injecting the
  281. # explicit value, because no exception is raised in this
  282. # branch. This means that the inserted value will be fully
  283. # consumed.
  284. if explicit_value is not None:
  285. state.rargs.insert(0, explicit_value)
  286. nargs = option.nargs
  287. if len(state.rargs) < nargs:
  288. _error_opt_args(nargs, opt)
  289. elif nargs == 1:
  290. value = state.rargs.pop(0)
  291. else:
  292. value = tuple(state.rargs[:nargs])
  293. del state.rargs[:nargs]
  294. elif explicit_value is not None:
  295. raise BadOptionUsage(opt, "{} option does not take a value".format(opt))
  296. else:
  297. value = None
  298. option.process(value, state)
  299. def _match_short_opt(self, arg, state):
  300. stop = False
  301. i = 1
  302. prefix = arg[0]
  303. unknown_options = []
  304. for ch in arg[1:]:
  305. opt = normalize_opt(prefix + ch, self.ctx)
  306. option = self._short_opt.get(opt)
  307. i += 1
  308. if not option:
  309. if self.ignore_unknown_options:
  310. unknown_options.append(ch)
  311. continue
  312. raise NoSuchOption(opt, ctx=self.ctx)
  313. if option.takes_value:
  314. # Any characters left in arg? Pretend they're the
  315. # next arg, and stop consuming characters of arg.
  316. if i < len(arg):
  317. state.rargs.insert(0, arg[i:])
  318. stop = True
  319. nargs = option.nargs
  320. if len(state.rargs) < nargs:
  321. _error_opt_args(nargs, opt)
  322. elif nargs == 1:
  323. value = state.rargs.pop(0)
  324. else:
  325. value = tuple(state.rargs[:nargs])
  326. del state.rargs[:nargs]
  327. else:
  328. value = None
  329. option.process(value, state)
  330. if stop:
  331. break
  332. # If we got any unknown options we re-combinate the string of the
  333. # remaining options and re-attach the prefix, then report that
  334. # to the state as new larg. This way there is basic combinatorics
  335. # that can be achieved while still ignoring unknown arguments.
  336. if self.ignore_unknown_options and unknown_options:
  337. state.largs.append("{}{}".format(prefix, "".join(unknown_options)))
  338. def _process_opts(self, arg, state):
  339. explicit_value = None
  340. # Long option handling happens in two parts. The first part is
  341. # supporting explicitly attached values. In any case, we will try
  342. # to long match the option first.
  343. if "=" in arg:
  344. long_opt, explicit_value = arg.split("=", 1)
  345. else:
  346. long_opt = arg
  347. norm_long_opt = normalize_opt(long_opt, self.ctx)
  348. # At this point we will match the (assumed) long option through
  349. # the long option matching code. Note that this allows options
  350. # like "-foo" to be matched as long options.
  351. try:
  352. self._match_long_opt(norm_long_opt, explicit_value, state)
  353. except NoSuchOption:
  354. # At this point the long option matching failed, and we need
  355. # to try with short options. However there is a special rule
  356. # which says, that if we have a two character options prefix
  357. # (applies to "--foo" for instance), we do not dispatch to the
  358. # short option code and will instead raise the no option
  359. # error.
  360. if arg[:2] not in self._opt_prefixes:
  361. return self._match_short_opt(arg, state)
  362. if not self.ignore_unknown_options:
  363. raise
  364. state.largs.append(arg)