安装
# pip3 install shyaml
帮助
# shyaml --help
Parses and output chosen subpart or values from YAML input.
It reads YAML in stdin and will output on stdout it's return value.
Usage:
shyaml {-h|--help}
shyaml {-V|--version}
shyaml [-y|--yaml] [-q|--quiet] ACTION KEY [DEFAULT]
Options:
-y, --yaml
Output only YAML safe value, more precisely, even
literal values will be YAML quoted. This behavior
is required if you want to output YAML subparts and
further process it. If you know you have are dealing
with safe literal value, then you don't need this.
(Default: no safe YAML output)
-q, --quiet
In case KEY value queried is an invalid path, quiet
mode will prevent the writing of an error message on
standard error.
(Default: no quiet mode)
-L, --line-buffer
Force parsing stdin line by line allowing to process
streamed YAML as it is fed instead of buffering
input and treating several YAML streamed document
at once. This is likely to have some small performance
hit if you have a huge stream of YAML document, but
then you probably don't really care about the
line-buffering.
(Default: no line buffering)
ACTION Depending on the type of data you've targetted
thanks to the KEY, ACTION can be:
These ACTIONs applies to any YAML type:
get-type ## returns a short string
get-value ## returns YAML
These ACTIONs applies to 'sequence' and 'struct' YAML type:
get-values{,-0} ## returns list of YAML
get-length ## returns an integer
These ACTION applies to 'struct' YAML type:
keys{,-0} ## returns list of YAML
values{,-0} ## returns list of YAML
key-values,{,-0} ## returns list of YAML
Note that any value returned is returned on stdout, and
when returning ``list of YAML``, it'll be separated by
a newline or ``NUL`` char depending of you've used the
``-0`` suffixed ACTION.
KEY Identifier to browse and target subvalues into YAML
structure. Use ``.`` to parse a subvalue. If you need
to use a literal ``.`` or ``\``, use ``\`` to quote it.
Use struct keyword to browse ``struct`` YAML data and use
integers to browse ``sequence`` YAML data.
DEFAULT if not provided and given KEY do not match any value in
the provided YAML, then DEFAULT will be returned. If no
default is provided and the KEY do not match any value
in the provided YAML, shyaml will fail with an error
message.
Examples:
## get last grocery
cat recipe.yaml | shyaml get-value groceries.-1
## get all words of my french dictionary
cat dictionaries.yaml | shyaml keys-0 french.dictionary
## get YAML config part of 'myhost'
cat hosts_config.yaml | shyaml get-value cfgs.myhost