shellsplit

package
v0.65.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shellsplit cuts a command line into the commands a shell would run, by that shell's own rules: what separates two commands, what quotes and escapes hide a separator, which text is data rather than a command (a heredoc body, a here-string, a comment), and which code is nested inside a command (command substitution, a PowerShell subexpression or script block, a heredoc body that is expanded or fed to an interpreter).

It is for scoring, not for executing: a nested command is returned as a command of its own as well as inside the one that holds it, and a line the rules can't finish (an unclosed quote) still yields what was read. Where the rules are ambiguous it errs toward showing a command, never toward hiding one.

scripts/shellsplit.py is a line-by-line port; testdata/golden.jsonl holds the two to the same output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Code

func Code(d Dialect, line string) string

Code returns line as a program, without its data (see Parsed.Code).

func Split

func Split(d Dialect, line string) []string

Split returns the commands in line (see Parsed.Commands).

func Trim

func Trim(s string) string

Trim is strings.TrimSpace — named so the Python port can match it exactly (Python's str.strip also drops \x1c-\x1f, which Go keeps).

Types

type Dialect

type Dialect int

Dialect is the shell a command line is written for.

const (
	Sh         Dialect = iota // POSIX sh and its kin (dash, bash, zsh): what `run` uses off Windows
	PowerShell                // Windows PowerShell 5.1 and PowerShell 7
	Cmd                       // cmd.exe
)

func (Dialect) String

func (d Dialect) String() string

String is the dialect's name as data rows spell it: sh, powershell, cmd.

type Parsed

type Parsed struct {
	// Commands, in order, each trimmed; nested ones follow the one holding them.
	Commands []string
	// Groups are commands joined by | || && (|& in sh) into one unit with two
	// or more members — a pipeline's danger can be in the joining (`curl … | sh`).
	Groups []string
	// Code is the line without its data — heredoc bodies, PowerShell
	// here-strings, comments — with its commands and the operators between
	// them. (A sh <<< here-string is one word of its command and stays.)
	Code string
	// Incomplete: code nested deeper than maxDepth, or past maxParses, was
	// kept as text, not cut.
	Incomplete bool
	// contains filtered or unexported fields
}

Parsed is a line cut into what is scored.

func Parse

func Parse(d Dialect, line string) Parsed

Parse cuts line once.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL