class documentation

class ParserContext: (source)

View In Hierarchy

Parsing context with knowledge of flags & their format. Generally associated with the core program or a task. When run through a parser, will also hold runtime values filled in by the parser. .. versionadded:: 1.0

Method __init__ Create a new ``ParserContext`` named ``name``, with ``aliases``.
Method __repr__ Undocumented
Method add_arg Adds given ``Argument`` (or constructor args for one) to this context.
Method flag_names Similar to `help_tuples` but returns flag names only, no helpstrs.
Method help_for Return 2-tuple of ``(flag-spec, help-string)`` for given ``flag``.
Method help_tuples Return sorted iterable of help tuples for all member Arguments.
Method names_for Undocumented
Instance Variable aliases Undocumented
Instance Variable args Undocumented
Instance Variable flags Undocumented
Instance Variable inverse_flags Undocumented
Instance Variable name Undocumented
Instance Variable positional_args Undocumented
Property as_kwargs This context's arguments' values keyed by their ``.name`` attribute.
Property missing_positional_args Undocumented
def __init__(self, name=None, aliases=(), args=()): (source)

Create a new ``ParserContext`` named ``name``, with ``aliases``. ``name`` is optional, and should be a string if given. It's used to tell ParserContext objects apart, and for use in a Parser when determining what chunk of input might belong to a given ParserContext. ``aliases`` is also optional and should be an iterable containing strings. Parsing will honor any aliases when trying to "find" a given context in its input. May give one or more ``args``, which is a quick alternative to calling ``for arg in args: self.add_arg(arg)`` after initialization.

def __repr__(self): (source)

Undocumented

def add_arg(self, *args, **kwargs): (source)

Adds given ``Argument`` (or constructor args for one) to this context. The Argument in question is added to the following dict attributes: * ``args``: "normal" access, i.e. the given names are directly exposed as keys. * ``flags``: "flaglike" access, i.e. the given names are translated into CLI flags, e.g. ``"foo"`` is accessible via ``flags['--foo']``. * ``inverse_flags``: similar to ``flags`` but containing only the "inverse" versions of boolean flags which default to True. This allows the parser to track e.g. ``--no-myflag`` and turn it into a False value for the ``myflag`` Argument. .. versionadded:: 1.0

def flag_names(self): (source)

Similar to `help_tuples` but returns flag names only, no helpstrs. Specifically, all flag names, flattened, in rough order. .. versionadded:: 1.0

def help_for(self, flag): (source)

Return 2-tuple of ``(flag-spec, help-string)`` for given ``flag``. .. versionadded:: 1.0

def help_tuples(self): (source)

Return sorted iterable of help tuples for all member Arguments. Sorts like so: * General sort is alphanumerically * Short flags win over long flags * Arguments with *only* long flags and *no* short flags will come first. * When an Argument has multiple long or short flags, it will sort using the most favorable (lowest alphabetically) candidate. This will result in a help list like so:: --alpha, --zeta # 'alpha' wins --beta -a, --query # short flag wins -b, --argh -c .. versionadded:: 1.0

def names_for(self, flag): (source)

Undocumented

Undocumented

Undocumented

Undocumented

inverse_flags: dict = (source)

Undocumented

Undocumented

positional_args: list = (source)

Undocumented

This context's arguments' values keyed by their ``.name`` attribute. Results in a dict suitable for use in Python contexts, where e.g. an arg named ``foo-bar`` becomes accessible as ``foo_bar``. .. versionadded:: 1.0

@property
missing_positional_args = (source)

Undocumented