Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

attr structured for core

Attribute for getting structured output from an external command.

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:

inputoutput
nothingrecord<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 file

Get 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 num

Get 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 name

Get 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 != Running

Notes

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.