https://github.com/anki-code/xontrib-dalias
Library of decorator aliases (daliases) for @xonsh.
https://github.com/anki-code/xontrib-dalias
xonsh xontrib
Last synced: 29 days ago
JSON representation
Library of decorator aliases (daliases) for @xonsh.
- Host: GitHub
- URL: https://github.com/anki-code/xontrib-dalias
- Owner: anki-code
- License: mit
- Created: 2024-06-24T00:43:19.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-02-17T16:50:57.000Z (2 months ago)
- Last Synced: 2025-03-13T07:41:12.576Z (about 1 month ago)
- Topics: xonsh, xontrib
- Language: Python
- Homepage:
- Size: 39.1 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Library of decorator aliases (daliases) for the xonsh shell.
If you like the idea click ⭐ on the repo and tweet.## Installation
To install use pip:
```xsh
xpip install xontrib-dalias
# or
xpip install 'xontrib-dalias[dict,yaml]' # With extra decorators.
```
Load:
```xsh
xontrib load dalias
```## Usage
### Transform output to object
Decorators:
* Default:
* `@lines` - return list of lines.
* `@json` - json to Python `dict`.
* `@path` - string to [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html).
* `@parts` - split by whitespaces using xonsh lexer. It's the same as [builtin `@$()` operator](https://xon.sh/tutorial.html#command-substitution-with).
* Extra (`xpip install 'xontrib-dalias[dict,yaml]'`):
* `@dict` - dict-like object (json, JavaScript object, Python dict) to Python `dict`.
* `@yaml` - YAML to Python `dict`.#### Examples
```xsh
$(@lines ls /)
# ['/bin', '/etc', '/home']$(@json echo '{"a":1}') # Try with `curl` ;)
# dict({"a":1})docker inspect @($(@json docker ps --format json)['ID'])
# Container info$(@path which -s xonsh)
# Path('/path/to/xonsh')$(@path which -s xonsh).parent
# Path('/path/to')aliases['ydig'] = '@yaml dig +yaml' # Update `dig` via `brew install bind` to have `+yaml`.
y = $(ydig google.com)
y[0]['type']
# 'MESSAGE'
```Piping into decorated alias to get object:
```xsh
$(echo '{"a":1}' | @json cat)
# dict({"a":1})
``````xsh
aliases['@j'] = '@json cat'
$(echo '{"a":1}' | @j)
# dict({"a":1})
```### Error handling
Default decorators:
* `@err` - set `$RAISE_SUBPROC_ERROR=True` for the command.
* `@noerr` - set `$RAISE_SUBPROC_ERROR=False` for the command.Examples:
```xsh
$RAISE_SUBPROC_ERROR = True # General environment.
if ![@noerr ls nononofile]: # Do not raise exception in case of error.
echo file
```## Credits
This package was created with [xontrib template](https://github.com/xonsh/xontrib-template).