Skip to content

yapx.Context

An immutable object that is passed to arguments annotated with yapx.Context, giving them access to the argument parser, raw arguments, parsed namespace, and any relay value.

ATTRIBUTE DESCRIPTION
parser

the root argparse ArgumentParser

TYPE: ArgumentParser

subparser

this command's subparser

TYPE: Optional[ArgumentParser]

args

raw command-line arguments

TYPE: List[str]

namespace

parsed command-line arguments

TYPE: Namespace

relay_value

Any value returned from the root command

TYPE: Any

Examples:

>>> import yapx
...
>>> def print_nums(*args: int):
...     print('Args: ', *args)
...     return args
...
>>> def find_evens(_context: yapx.Context):
...     return [x for x in _context.relay_value if x % 2 == 0]
...
>>> def find_odds(_context: yapx.Context):
...     return [x for x in _context.relay_value if x % 2 != 0]
...
>>> cli_args = ['find-odds', '1', '2', '3', '4', '5']
>>> yapx.run(print_nums, [find_evens, find_odds], args=cli_args)
Args:  1 2 3 4 5
[1, 3, 5]