create a custom context parser permalink

parse custom cli arguments permalink

A context_parser parses the pypyr cli input arguments. Simply put, this is all the positional arguments after the pipeline-name in the cli.

$ pypyr pipelinename this is the args input

In this example, ['this', 'is', 'the', 'args', 'input'] will go to the pipeline’s context parser as input.

Generally, a context_parser is likely to take the input args list and create a dict with it somehow. pypyr will use this dict to initialize context for the pipeline run.

For real-life inspiration, see the actual code for pypyr’s built-in context parsers.

parser function signature & example permalink

A custom parser is any Python module containing a function with this signature:

get_parsed_context(args: list[str] | None) -> Mapping | None

Here is a more fleshed out example that you can copy & paste to get started:

from collections.abc import Mapping
import logging

# getLogger will grab the parent logger context, so your loglevel and
# formatting will inherit correctly automatically from the pypyr core.
logger = logging.getLogger(__name__)


def get_parsed_context(args: list[str] | None) -> Mapping | None:
    """This is the signature for a context parser.

    Args:
      args: list of string. Passed from command-line invocation where:
            $ pypyr pipelinename this is the context_arg
            This would result in args == ['this', 'is', 'the', 'context_arg']

    Returns:
      dict. This dict will initialize the context for the pipeline run.
    """
    assert args, ("you must invoke pipeline with context arg set.")
    logger.debug("starting")

    # your clever code here. Chances are pretty good you'll be doing things
    # with the input args list to create a dictionary.

    # function signature returns a dictionary
    return {'key1': 'value1', 'key2':'value2'}

use custom parser in a pipeline permalink

The usual custom module import resolution rules apply.

|- myproj
  |- mypipeline.yaml
  |- mydir/
    |- myparser.py

Assuming you saved your python with the def get_parsed_context(args) in a file like this {pipeline dir}/mydir/myparser.py, you can use use it in your pipeline like this:

context_parser: mydir.myparser
steps:
    - step1
    - step2

If you package your parser and you install the package into the active python environment, you can of course use the usual python package name instead:

context_parser: mypackage.myparser
steps:
    - step1
    - step2

see also

last updated on .