]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/click/parser.py
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).
7 The plan is to remove more and more from here over time.
9 The reason this is a different module and not optparse from the stdlib
10 is that there are differences in 2.x and 3.x about the error messages
11 generated and optparse in the stdlib uses gettext for no good reason
12 and might cause us issues.
14 Click uses parts of optparse written by Gregory P. Ward and maintained
15 by the Python Software Foundation. This is limited to code in parser.py.
17 Copyright 2001-2006 Gregory P. Ward. All rights reserved.
18 Copyright 2002-2006 Python Software Foundation. All rights reserved.
20 # This code uses parts of optparse written by Gregory P. Ward and
21 # maintained by the Python Software Foundation.
22 # Copyright 2001-2006 Gregory P. Ward
23 # Copyright 2002-2006 Python Software Foundation
25 from collections
import deque
26 from gettext
import gettext
as _
27 from gettext
import ngettext
29 from .exceptions
import BadArgumentUsage
30 from .exceptions
import BadOptionUsage
31 from .exceptions
import NoSuchOption
32 from .exceptions
import UsageError
35 import typing_extensions
as te
36 from .core
import Argument
as CoreArgument
37 from .core
import Context
38 from .core
import Option
as CoreOption
39 from .core
import Parameter
as CoreParameter
43 # Sentinel value that indicates an option was passed as a flag without a
44 # value but is not a flag option. Option.consume_value uses this to
45 # prompt or use the flag_value.
46 _flag_needs_value
= object()
50 args
: t
.Sequence
[str], nargs_spec
: t
.Sequence
[int]
51 ) -> t
.Tuple
[t
.Sequence
[t
.Union
[str, t
.Sequence
[t
.Optional
[str]], None]], t
.List
[str]]:
52 """Given an iterable of arguments and an iterable of nargs specifications,
53 it returns a tuple with all the unpacked arguments at the first index
54 and all remaining arguments as the second.
56 The nargs specification is the number of arguments that should be consumed
57 or `-1` to indicate that this position should eat up all the remainders.
59 Missing items are filled with `None`.
62 nargs_spec
= deque(nargs_spec
)
63 rv
: t
.List
[t
.Union
[str, t
.Tuple
[t
.Optional
[str], ...], None]] = []
64 spos
: t
.Optional
[int] = None
66 def _fetch(c
: "te.Deque[V]") -> t
.Optional
[V
]:
76 nargs
= _fetch(nargs_spec
)
82 rv
.append(_fetch(args
))
84 x
= [_fetch(args
) for _
in range(nargs
)]
86 # If we're reversed, we're pulling in the arguments in reverse,
87 # so we need to turn them around.
94 raise TypeError("Cannot have two nargs < 0")
99 # spos is the position of the wildcard (star). If it's not `None`,
100 # we fill it with the remainder.
102 rv
[spos
] = tuple(args
)
104 rv
[spos
+ 1 :] = reversed(rv
[spos
+ 1 :])
106 return tuple(rv
), list(args
)
109 def split_opt(opt
: str) -> t
.Tuple
[str, str]:
113 if opt
[1:2] == first
:
114 return opt
[:2], opt
[2:]
115 return first
, opt
[1:]
118 def normalize_opt(opt
: str, ctx
: t
.Optional
["Context"]) -> str:
119 if ctx
is None or ctx
.token_normalize_func
is None:
121 prefix
, opt
= split_opt(opt
)
122 return f
"{prefix}{ctx.token_normalize_func(opt)}"
125 def split_arg_string(string
: str) -> t
.List
[str]:
126 """Split an argument string as with :func:`shlex.split`, but don't
127 fail if the string is incomplete. Ignores a missing closing quote or
128 incomplete escape sequence and uses the partial token as-is.
130 .. code-block:: python
132 split_arg_string("example 'my file")
133 ["example", "my file"]
135 split_arg_string("example my\\")
138 :param string: String to split.
142 lex
= shlex
.shlex(string
, posix
=True)
143 lex
.whitespace_split
= True
151 # Raised when end-of-string is reached in an invalid state. Use
152 # the partial token as-is. The quote or escape character is in
153 # lex.state, not lex.token.
154 out
.append(lex
.token
)
163 opts
: t
.Sequence
[str],
164 dest
: t
.Optional
[str],
165 action
: t
.Optional
[str] = None,
167 const
: t
.Optional
[t
.Any
] = None,
169 self
._short
_opts
= []
171 self
.prefixes
: t
.Set
[str] = set()
174 prefix
, value
= split_opt(opt
)
176 raise ValueError(f
"Invalid start character for option ({opt})")
177 self
.prefixes
.add(prefix
[0])
178 if len(prefix
) == 1 and len(value
) == 1:
179 self
._short
_opts
.append(opt
)
181 self
._long
_opts
.append(opt
)
182 self
.prefixes
.add(prefix
)
194 def takes_value(self
) -> bool:
195 return self
.action
in ("store", "append")
197 def process(self
, value
: t
.Any
, state
: "ParsingState") -> None:
198 if self
.action
== "store":
199 state
.opts
[self
.dest
] = value
# type: ignore
200 elif self
.action
== "store_const":
201 state
.opts
[self
.dest
] = self
.const
# type: ignore
202 elif self
.action
== "append":
203 state
.opts
.setdefault(self
.dest
, []).append(value
) # type: ignore
204 elif self
.action
== "append_const":
205 state
.opts
.setdefault(self
.dest
, []).append(self
.const
) # type: ignore
206 elif self
.action
== "count":
207 state
.opts
[self
.dest
] = state
.opts
.get(self
.dest
, 0) + 1 # type: ignore
209 raise ValueError(f
"unknown action '{self.action}'")
210 state
.order
.append(self
.obj
)
214 def __init__(self
, obj
: "CoreArgument", dest
: t
.Optional
[str], nargs
: int = 1):
221 value
: t
.Union
[t
.Optional
[str], t
.Sequence
[t
.Optional
[str]]],
222 state
: "ParsingState",
225 assert value
is not None
226 holes
= sum(1 for x
in value
if x
is None)
227 if holes
== len(value
):
230 raise BadArgumentUsage(
231 _("Argument {name!r} takes {nargs} values.").format(
232 name
=self
.dest
, nargs
=self
.nargs
236 if self
.nargs
== -1 and self
.obj
.envvar
is not None and value
== ():
237 # Replace empty tuple with None so that a value from the
238 # environment may be tried.
241 state
.opts
[self
.dest
] = value
# type: ignore
242 state
.order
.append(self
.obj
)
246 def __init__(self
, rargs
: t
.List
[str]) -> None:
247 self
.opts
: t
.Dict
[str, t
.Any
] = {}
248 self
.largs
: t
.List
[str] = []
250 self
.order
: t
.List
["CoreParameter"] = []
254 """The option parser is an internal class that is ultimately used to
255 parse options and arguments. It's modelled after optparse and brings
256 a similar but vastly simplified API. It should generally not be used
257 directly as the high level Click classes wrap it for you.
259 It's not nearly as extensible as optparse or argparse as it does not
260 implement features that are implemented on a higher level (such as
263 :param ctx: optionally the :class:`~click.Context` where this parser
267 def __init__(self
, ctx
: t
.Optional
["Context"] = None) -> None:
268 #: The :class:`~click.Context` for this parser. This might be
269 #: `None` for some advanced use cases.
271 #: This controls how the parser deals with interspersed arguments.
272 #: If this is set to `False`, the parser will stop on the first
273 #: non-option. Click uses this to implement nested subcommands
275 self
.allow_interspersed_args
: bool = True
276 #: This tells the parser how to deal with unknown options. By
277 #: default it will error out (which is sensible), but there is a
278 #: second mode where it will ignore it and continue processing
279 #: after shifting all the unknown options into the resulting args.
280 self
.ignore_unknown_options
: bool = False
283 self
.allow_interspersed_args
= ctx
.allow_interspersed_args
284 self
.ignore_unknown_options
= ctx
.ignore_unknown_options
286 self
._short
_opt
: t
.Dict
[str, Option
] = {}
287 self
._long
_opt
: t
.Dict
[str, Option
] = {}
288 self
._opt
_prefixes
= {"-", "--"}
289 self
._args
: t
.List
[Argument
] = []
294 opts
: t
.Sequence
[str],
295 dest
: t
.Optional
[str],
296 action
: t
.Optional
[str] = None,
298 const
: t
.Optional
[t
.Any
] = None,
300 """Adds a new option named `dest` to the parser. The destination
301 is not inferred (unlike with optparse) and needs to be explicitly
302 provided. Action can be any of ``store``, ``store_const``,
303 ``append``, ``append_const`` or ``count``.
305 The `obj` can be used to identify the option in the order list
306 that is returned from the parser.
308 opts
= [normalize_opt(opt
, self
.ctx
) for opt
in opts
]
309 option
= Option(obj
, opts
, dest
, action
=action
, nargs
=nargs
, const
=const
)
310 self
._opt
_prefixes
.update(option
.prefixes
)
311 for opt
in option
._short
_opts
:
312 self
._short
_opt
[opt
] = option
313 for opt
in option
._long
_opts
:
314 self
._long
_opt
[opt
] = option
317 self
, obj
: "CoreArgument", dest
: t
.Optional
[str], nargs
: int = 1
319 """Adds a positional argument named `dest` to the parser.
321 The `obj` can be used to identify the option in the order list
322 that is returned from the parser.
324 self
._args
.append(Argument(obj
, dest
=dest
, nargs
=nargs
))
327 self
, args
: t
.List
[str]
328 ) -> t
.Tuple
[t
.Dict
[str, t
.Any
], t
.List
[str], t
.List
["CoreParameter"]]:
329 """Parses positional arguments and returns ``(values, args, order)``
330 for the parsed options and arguments as well as the leftover
331 arguments if there are any. The order is a list of objects as they
332 appear on the command line. If arguments appear multiple times they
333 will be memorized multiple times as well.
335 state
= ParsingState(args
)
337 self
._process
_args
_for
_options
(state
)
338 self
._process
_args
_for
_args
(state
)
340 if self
.ctx
is None or not self
.ctx
.resilient_parsing
:
342 return state
.opts
, state
.largs
, state
.order
344 def _process_args_for_args(self
, state
: ParsingState
) -> None:
345 pargs
, args
= _unpack_args(
346 state
.largs
+ state
.rargs
, [x
.nargs
for x
in self
._args
]
349 for idx
, arg
in enumerate(self
._args
):
350 arg
.process(pargs
[idx
], state
)
355 def _process_args_for_options(self
, state
: ParsingState
) -> None:
357 arg
= state
.rargs
.pop(0)
359 # Double dashes always handled explicitly regardless of what
360 # prefixes are valid.
363 elif arg
[:1] in self
._opt
_prefixes
and arglen
> 1:
364 self
._process
_opts
(arg
, state
)
365 elif self
.allow_interspersed_args
:
366 state
.largs
.append(arg
)
368 state
.rargs
.insert(0, arg
)
371 # Say this is the original argument list:
372 # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]
374 # (we are about to process arg(i)).
376 # Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of
377 # [arg0, ..., arg(i-1)] (any options and their arguments will have
378 # been removed from largs).
380 # The while loop will usually consume 1 or more arguments per pass.
381 # If it consumes 1 (eg. arg is an option that takes no arguments),
382 # then after _process_arg() is done the situation is:
384 # largs = subset of [arg0, ..., arg(i)]
385 # rargs = [arg(i+1), ..., arg(N-1)]
387 # If allow_interspersed_args is false, largs will always be
388 # *empty* -- still a subset of [arg0, ..., arg(i-1)], but
389 # not a very interesting subset!
392 self
, opt
: str, explicit_value
: t
.Optional
[str], state
: ParsingState
394 if opt
not in self
._long
_opt
:
395 from difflib
import get_close_matches
397 possibilities
= get_close_matches(opt
, self
._long
_opt
)
398 raise NoSuchOption(opt
, possibilities
=possibilities
, ctx
=self
.ctx
)
400 option
= self
._long
_opt
[opt
]
401 if option
.takes_value
:
402 # At this point it's safe to modify rargs by injecting the
403 # explicit value, because no exception is raised in this
404 # branch. This means that the inserted value will be fully
406 if explicit_value
is not None:
407 state
.rargs
.insert(0, explicit_value
)
409 value
= self
._get
_value
_from
_state
(opt
, option
, state
)
411 elif explicit_value
is not None:
412 raise BadOptionUsage(
413 opt
, _("Option {name!r} does not take a value.").format(name
=opt
)
419 option
.process(value
, state
)
421 def _match_short_opt(self
, arg
: str, state
: ParsingState
) -> None:
428 opt
= normalize_opt(f
"{prefix}{ch}", self
.ctx
)
429 option
= self
._short
_opt
.get(opt
)
433 if self
.ignore_unknown_options
:
434 unknown_options
.append(ch
)
436 raise NoSuchOption(opt
, ctx
=self
.ctx
)
437 if option
.takes_value
:
438 # Any characters left in arg? Pretend they're the
439 # next arg, and stop consuming characters of arg.
441 state
.rargs
.insert(0, arg
[i
:])
444 value
= self
._get
_value
_from
_state
(opt
, option
, state
)
449 option
.process(value
, state
)
454 # If we got any unknown options we recombine the string of the
455 # remaining options and re-attach the prefix, then report that
456 # to the state as new larg. This way there is basic combinatorics
457 # that can be achieved while still ignoring unknown arguments.
458 if self
.ignore_unknown_options
and unknown_options
:
459 state
.largs
.append(f
"{prefix}{''.join(unknown_options)}")
461 def _get_value_from_state(
462 self
, option_name
: str, option
: Option
, state
: ParsingState
466 if len(state
.rargs
) < nargs
:
467 if option
.obj
._flag
_needs
_value
:
468 # Option allows omitting the value.
469 value
= _flag_needs_value
471 raise BadOptionUsage(
474 "Option {name!r} requires an argument.",
475 "Option {name!r} requires {nargs} arguments.",
477 ).format(name
=option_name
, nargs
=nargs
),
480 next_rarg
= state
.rargs
[0]
483 option
.obj
._flag
_needs
_value
484 and isinstance(next_rarg
, str)
485 and next_rarg
[:1] in self
._opt
_prefixes
486 and len(next_rarg
) > 1
488 # The next arg looks like the start of an option, don't
489 # use it as the value if omitting the value is allowed.
490 value
= _flag_needs_value
492 value
= state
.rargs
.pop(0)
494 value
= tuple(state
.rargs
[:nargs
])
495 del state
.rargs
[:nargs
]
499 def _process_opts(self
, arg
: str, state
: ParsingState
) -> None:
500 explicit_value
= None
501 # Long option handling happens in two parts. The first part is
502 # supporting explicitly attached values. In any case, we will try
503 # to long match the option first.
505 long_opt
, explicit_value
= arg
.split("=", 1)
508 norm_long_opt
= normalize_opt(long_opt
, self
.ctx
)
510 # At this point we will match the (assumed) long option through
511 # the long option matching code. Note that this allows options
512 # like "-foo" to be matched as long options.
514 self
._match
_long
_opt
(norm_long_opt
, explicit_value
, state
)
516 # At this point the long option matching failed, and we need
517 # to try with short options. However there is a special rule
518 # which says, that if we have a two character options prefix
519 # (applies to "--foo" for instance), we do not dispatch to the
520 # short option code and will instead raise the no option
522 if arg
[:2] not in self
._opt
_prefixes
:
523 self
._match
_short
_opt
(arg
, state
)
526 if not self
.ignore_unknown_options
:
529 state
.largs
.append(arg
)