create a custom context parser
parse custom cli arguments
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.
parser function signature & example
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):
"""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, ("pipeline must be invoked 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
The usual python import module resolution rules apply. pypyr will resolve
modules from the
working directory
first, which you can change by using the --dir
flag.
Assuming you saved your python with the def get_parsed_context(args)
in a
file like this ./dir/myparser.py
, you can use use it in your pipeline like
this:
context_parser: dir.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