MCPcopy
hub / github.com/pallets/click / _OptionParser

Class _OptionParser

src/click/parser.py:224–500  ·  view source on GitHub ↗

The option parser is an internal class that is ultimately used to parse options and arguments. It's modelled after optparse and brings a similar but vastly simplified API. It should generally not be used directly as the high level Click classes wrap it for you. It's not nearly as

Source from the content-addressed store, hash-verified

222
223
224class _OptionParser:
225 """The option parser is an internal class that is ultimately used to
226 parse options and arguments. It's modelled after optparse and brings
227 a similar but vastly simplified API. It should generally not be used
228 directly as the high level Click classes wrap it for you.
229
230 It's not nearly as extensible as optparse or argparse as it does not
231 implement features that are implemented on a higher level (such as
232 types or defaults).
233
234 :param ctx: optionally the :class:`~click.Context` where this parser
235 should go with.
236
237 .. deprecated:: 8.2
238 Will be removed in Click 9.0.
239 """
240
241 def __init__(self, ctx: Context | None = None) -> None:
242 #: The :class:`~click.Context` for this parser. This might be
243 #: `None` for some advanced use cases.
244 self.ctx = ctx
245 #: This controls how the parser deals with interspersed arguments.
246 #: If this is set to `False`, the parser will stop on the first
247 #: non-option. Click uses this to implement nested subcommands
248 #: safely.
249 self.allow_interspersed_args: bool = True
250 #: This tells the parser how to deal with unknown options. By
251 #: default it will error out (which is sensible), but there is a
252 #: second mode where it will ignore it and continue processing
253 #: after shifting all the unknown options into the resulting args.
254 self.ignore_unknown_options: bool = False
255
256 if ctx is not None:
257 self.allow_interspersed_args = ctx.allow_interspersed_args
258 self.ignore_unknown_options = ctx.ignore_unknown_options
259
260 self._short_opt: dict[str, _Option] = {}
261 self._long_opt: dict[str, _Option] = {}
262 self._opt_prefixes = {"-", "--"}
263 self._args: list[_Argument] = []
264
265 def add_option(
266 self,
267 obj: CoreOption,
268 opts: cabc.Sequence[str],
269 dest: str | None,
270 action: str | None = None,
271 nargs: int = 1,
272 const: t.Any | None = None,
273 ) -> None:
274 """Adds a new option named `dest` to the parser. The destination
275 is not inferred (unlike with optparse) and needs to be explicitly
276 provided. Action can be any of ``store``, ``store_const``,
277 ``append``, ``append_const`` or ``count``.
278
279 The `obj` can be used to identify the option in the order list
280 that is returned from the parser.
281 """

Callers 3

make_parserMethod · 0.85

Calls

no outgoing calls

Tested by 2