MCPcopy Index your code
hub / github.com/pyinvoke/invoke / Parser

Class Parser

invoke/parser/parser.py:47–202  ·  view source on GitHub ↗

Create parser conscious of ``contexts`` and optional ``initial`` context. ``contexts`` should be an iterable of ``Context`` instances which will be searched when new context names are encountered during a parse. These Contexts determine what flags may follow them, as well as whethe

Source from the content-addressed store, hash-verified

45
46
47class Parser:
48 """
49 Create parser conscious of ``contexts`` and optional ``initial`` context.
50
51 ``contexts`` should be an iterable of ``Context`` instances which will be
52 searched when new context names are encountered during a parse. These
53 Contexts determine what flags may follow them, as well as whether given
54 flags take values.
55
56 ``initial`` is optional and will be used to determine validity of "core"
57 options/flags at the start of the parse run, if any are encountered.
58
59 ``ignore_unknown`` determines what to do when contexts are found which do
60 not map to any members of ``contexts``. By default it is ``False``, meaning
61 any unknown contexts result in a parse error exception. If ``True``,
62 encountering an unknown context halts parsing and populates the return
63 value's ``.unparsed`` attribute with the remaining parse tokens.
64
65 .. versionadded:: 1.0
66 """
67
68 def __init__(
69 self,
70 contexts: Iterable["ParserContext"] = (),
71 initial: Optional["ParserContext"] = None,
72 ignore_unknown: bool = False,
73 ) -> None:
74 self.initial = initial
75 self.contexts = Lexicon()
76 self.ignore_unknown = ignore_unknown
77 for context in contexts:
78 debug("Adding {}".format(context))
79 if not context.name:
80 raise ValueError("Non-initial contexts must have names.")
81 exists = "A context named/aliased {!r} is already in this parser!"
82 if context.name in self.contexts:
83 raise ValueError(exists.format(context.name))
84 self.contexts[context.name] = context
85 for alias in context.aliases:
86 if alias in self.contexts:
87 raise ValueError(exists.format(alias))
88 self.contexts.alias(alias, to=context.name)
89
90 def parse_argv(self, argv: List[str]) -> ParseResult:
91 """
92 Parse an argv-style token list ``argv``.
93
94 Returns a list (actually a subclass, `.ParseResult`) of
95 `.ParserContext` objects matching the order they were found in the
96 ``argv`` and containing `.Argument` objects with updated values based
97 on any flags given.
98
99 Assumes any program name has already been stripped out. Good::
100
101 Parser(...).parse_argv(['--core-opt', 'task', '--task-opt'])
102
103 Bad::
104

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…