attr structured for core
Command Type
built-in
Signature
> attr structured {flags} (args) (decode)
Parameters
args: Arguments that make the external command print structured output.decode: Turns that output, given as$in, into data.
Input/output types:
| input | output |
|---|---|
| nothing | record<args: list<string>, decode: closure> |
Examples
Get the changed files from git status --porcelain as a table.
> @structured [--porcelain] { lines | parse --regex '^(?<status>..) (?<file>.+)$' }
extern "git status" []
git status | where status =~ M | get fileGet the installed Python packages from pip list --format=json as a table.
> @structured [--format=json] { from json }
extern "pip list" []
pip list | where name starts-with numGet the outdated Homebrew formulae from brew outdated --json=v2 as a table.
> @structured [--json=v2] { from json | get formulae }
extern "brew outdated" []
brew outdated | where pinned == false | get nameGet pods from kubectl as a table when the output is piped into where.
> @structured [-o json] { from json | get items }
extern "kubectl get" [resource: string, --namespace(-n): string]: nothing -> table
kubectl get pods | where status.phase != RunningNotes
When the output of the extern is used by nushell (piped into an internal command or collected into a variable), the arguments are added right after the command's name and the output is decoded with the block. When the output goes to the terminal, print, a file, ignore, save, tee, complete or another external command (run-external included), the command runs as written. The same holds when the extern is the last command of a do block or a custom command. Calling the command with a caret, like ^git status, always runs it as written.
The arguments come before the user's own, so a format flag the user passes can override them and break the decode, as in kubectl get pods -o wide. Use a caret call for that. tee passes the command's own output on to the commands after it. When an extern has more than one @structured, the last one is used.
Like a def body, the block can read $in, $env, constants and the let variables declared before the extern, but not mut variables.