parser.py 19 KB

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