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.

Attributes:

Name Type Description
parser ArgumentParser

the root argparse ArgumentParser

subparser Optional[ArgumentParser]

this command's subparser

args List[str]

raw command-line arguments

namespace Namespace

parsed command-line arguments

relay_value Any

Any value returned from the root command

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]