pypyr.parser.dict
create a dict from key=value pair string
Takes a key=value pair string and returns a dictionary (aka a map) where each
pair becomes a dictionary element inside a dict with name argDict
.
Escape literal spaces with single or double quotes.
Any arg without an =
will parse to key: ''
.
$ pypyr my-pipeline arg0 key1=value1 key2=2 key3="value 3"
_
This will parse to context like this:
{
'argDict': {
'arg0': '',
'key1': 'value1',
'key2': '2',
'key3': 'value 3'}
}
example
$ pypyr my-pipeline param1=value1 param2="value 2" param3=value3
This will create a context dictionary like this:
{'argDict': {'param1': 'value1',
'param2': 'value 2',
'param3': 'value3'}}
Or to put in in yaml terms:
argDict:
param1: value1
param2: value 2
param3: value3
You can access the dict you create this way with formatting expressions:
context_parser: pypyr.parser.dict
steps:
- name: pypyr.steps.echo
in:
echoMe: param2 value is {argDict[param2]}
special characters
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.
$ pypyr my-pipeline param1=value1 param2='value 2' "param 3"=123
_
It will look like this in context:
{ 'argDict': {
'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:
$ pypyr my-pipeline key1='value !@£$%^&' key_2=nothing_special
_
Will initialize context like this:
{ 'argDict': {
'key1': 'value !@£$%^&',
'key_2': 'nothing_special'}
}
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:
$ pypyr my-pipeline k1=one+two=three k2=arb
_
Will result in:
{ 'argDict': {
'k1': 'one+two=three',
'k2': 'arb'}
}