pypyr.parser.argskwargs permalink

parse list of args & key=value pairs from cli permalink

Puts input cli arguments into a list argList. If an argument has a =, will save the key=value pair as a dictionary/mapping element.

term
$ pypyr my-pipeline arg1 arg2 k1=value1 k2=value2

$ pypyr my-pipeline arg1 "arg 2" k1="value1" k2="value 2"

The second example will result in your pipeline context looking like this:

{
  'argList': ['arg1', 'arg 2'],
  'k1': 'value1',
  'k2': 'value 2'
}

This gives you cli input syntax very similar to makefile. Alternatively, you can conceptualize this as a combination of pypyr.parser.list and pypyr.parser.keyvaluepairs.

Escape literal spaces with single or double quotes.

If you don’t pass any arguments or when you pass only key=value pairs, pypyr will initialize argList to an empty list [].

special characters permalink

Notice that you can have both keys and values with spaces by using either single or double quotes. If you need to escape quotes, refer to your shell’s documentation for escape characters.

term
$ pypyr my-pipeline param1=value1 param2='value 2' "param 3"=123
{'param1': 'value1', 'param2': 'value 2', 'param 3': '123'}

Although you can have spaces and special characters such as punctuation in your keys (such as “param 3” above), for an easy life you might want to avoid doing so.

The reason is that when you want to reference your context keys in {formatting expressions} or variables in py string expressions or in a pypyr.steps.py step you’re going to run into trouble - so it’s best to stick to keys that are valid Python identifiers.

Simply put, don’t have punctuation other than _ in your keys unless you’re really sure you’re willing to live with the side-effects.

While you might want to keep your keys as free of special characters as you can, the values can be whatever you want:

term
$ pypyr my-pipeline "list ^ value" key1='value !@£$%^&' key_2=nothing_special
_

Will parse like this:

{
  'argList': ['list ^ value'],
  'key1': 'value !@£$%^&',
  'key_2': 'nothing_special'
}

Notice that list values for argList can contain whatever special characters you want, without causing trouble for py-strings and the py step.

When parsing key=value pairs, pypyr only considers the first = as marking the key, subsequent = in the same arg will be considered part of the value:

term
$ pypyr my-pipeline k1=one+two=three k2=arb
_

Will result in:

{
  'k1': 'one+two=three',
  'k2': 'arb'
}

example permalink

Given a pipeline like this, arbitrarily saved as ./my-pipeline.yaml:

# ./my-pipeline.yaml
context_parser: pypyr.parser.argskwargs
steps:
  - pypyr.steps.debug # prints at log level <=20

You can then pass any combination of arguments and key-value pairs from the cli to the pipeline to initialize context.

term
$ pypyr my-pipeline arg1 arg2 k1=value1 k2="value 2" --log 20
{'argList': ['arg1', 'arg2'], 'k1': 'value1', 'k2': 'value 2'}

see also

last updated on .