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

Method parse_argv

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

Parse an argv-style token list ``argv``. Returns a list (actually a subclass, `.ParseResult`) of `.ParserContext` objects matching the order they were found in the ``argv`` and containing `.Argument` objects with updated values based on any flags given.

(self, argv: List[str])

Source from the content-addressed store, hash-verified

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
105 Parser(...).parse_argv(['invoke', '--core-opt', ...])
106
107 :param argv: List of argument string tokens.
108 :returns:
109 A `.ParseResult` (a ``list`` subclass containing some number of
110 `.ParserContext` objects).
111
112 .. versionadded:: 1.0
113 """
114 machine = ParseMachine(
115 # FIXME: initial should not be none
116 initial=self.initial, # type: ignore[arg-type]
117 contexts=self.contexts,
118 ignore_unknown=self.ignore_unknown,
119 )
120 # FIXME: Why isn't there str.partition for lists? There must be a
121 # better way to do this. Split argv around the double-dash remainder
122 # sentinel.
123 debug("Starting argv: {!r}".format(argv))
124 try:
125 ddash = argv.index("--")
126 except ValueError:
127 ddash = len(argv) # No remainder == body gets all
128 body = argv[:ddash]
129 remainder = argv[ddash:][1:] # [1:] to strip off remainder itself
130 if remainder:
131 debug(
132 "Remainder: argv[{!r}:][1:] => {!r}".format(ddash, remainder)
133 )
134 for index, token in enumerate(body):
135 # Handle non-space-delimited forms, if not currently expecting a
136 # flag value and still in valid parsing territory (i.e. not in
137 # "unknown" state which implies store-only)
138 # NOTE: we do this in a few steps so we can
139 # split-then-check-validity; necessary for things like when the
140 # previously seen flag optionally takes a value.
141 mutations = []
142 orig = token
143 if is_flag(token) and not machine.result.unparsed:
144 # Equals-sign-delimited flags, eg --foo=bar or -f=bar
145 if "=" in token:
146 token, _, value = token.partition("=")
147 msg = "Splitting x=y expr {!r} into tokens {!r} and {!r}"

Calls 5

handleMethod · 0.95
ParseMachineClass · 0.85
is_flagFunction · 0.85
is_long_flagFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected