parser.py 19 KB

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