shyaml: YAML for the Command Line (Python version)
The original `shyaml` Python library (v0.6.2) is a command-line tool designed for reading and querying YAML files. It enables shell scripts to easily access YAML data using simple dot-notation paths. While it remains available on PyPI, this Python version is in a low-maintenance state with no new features planned. Active development has transitioned to `shyaml-rs`, a Rust rewrite that offers significant performance improvements and additional features like write capabilities, while maintaining command-line interface compatibility.
Warnings
- gotcha The Python `shyaml` library is in maintenance mode. `shyaml-rs`, a Rust rewrite, is the actively developed and recommended replacement. `shyaml-rs` is designed as a drop-in CLI replacement, offering 14-42x faster performance and new write capabilities (e.g., `set-value`, `del`, `apply`) not present in the Python version.
- breaking Handling of missing paths changed significantly in version 0.3. Prior to 0.3, if a specified key was not found, `shyaml` would return an empty string by default. From version 0.3 onwards (inclusive), if a key is not found and no default value is explicitly provided as the third argument, `shyaml` will print an error message and exit.
- breaking Ordered mappings behavior changed in version 0.4.0. Before 0.4.0, `shyaml` would alphabetically re-order keys in mappings. While YAML specification considers mappings unordered, this behavior could be unexpected for users. From version 0.4.0 onwards, `shyaml` preserves the order of keys in mappings.
- gotcha By default, `shyaml` outputs simple string or number values directly without YAML quotes for convenience. This means the output is not strictly valid YAML in all cases. If you need strict YAML output (e.g., for piping to another YAML processor), you must use the `--yaml` or `-y` option.
Install
-
pip install shyaml
Imports
- shyaml.do
import shyaml # ... then call shyaml.do(stdin_stream, args)
Quickstart
cat <<EOF > config.yaml
database:
host: localhost
port: 5432
credentials:
username: admin
password: ${DB_PASSWORD:-fallback_pass}
EOF
DB_PASSWORD="secure_pass" \
cat config.yaml | shyaml get-value database.credentials.password