Parser: Core parsing state machine

class invoke.parser.ParseResult(*args, **kwargs)

List-like object with some extra parse-related attributes.

Specifically, a .remainder attribute, which is the string found after a -- in any parsed argv list; and an .unparsed attribute, a list of tokens that were unable to be parsed.

class invoke.parser.Parser(contexts=(), initial=None, ignore_unknown=False)

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 whether given flags take values.

initial is optional and will be used to determine validity of “core” options/flags at the start of the parse run, if any are encountered.

ignore_unknown determines what to do when contexts are found which do not map to any members of contexts. By default it is False, meaning any unknown contexts result in a parse error exception. If True, encountering an unknown context halts parsing and populates the return value’s .unparsed attribute with the remaining parse tokens.


Parse an argv-style token list argv.

Returns a list of Context objects matching the order they were found in the argv and containing Argument objects with updated values based on any flags given.

Assumes any program name has already been stripped out. Good:

Parser(...).parse_argv(['--core-opt', 'task', '--task-opt'])


Parser(...).parse_argv(['invoke', '--core-opt', ...])

Previous topic

Context: Task-oriented parser context

Next topic

Runner: Executes shell commands

This Page