shell

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArgWords

func ArgWords(cmd string) ([]string, bool)

ArgWords returns every word of every simple command in a line, which is what a rule about the line's arguments needs to consider. Assignment prefixes are not words and are not included.

func HasPipe

func HasPipe(cmd string) bool

HasPipe reports whether a line joins commands with a pipe, which is what decides whether a bare `-` argument is fed by an upstream command or would sit reading the session's empty stdin. A pipe character inside a quoted argument is not one, which is the reason this asks the parse rather than the string.

func MatchesWords

func MatchesWords(line, match string) bool

MatchesWords reports whether a line contains the match text as a run of consecutive whole words. Both sides are read as shell, so quoting is resolved the same way on each and a match written the way the document writes it selects the same line. When either side does not parse, the comparison falls back to a substring, which is the old behavior and the only thing left to do with text no parser can read.

func Operands

func Operands(cmd string) []string

Operands returns every word of a line that is not a command name, so a rule about a line's arguments never trips over the program itself. A tool named `pattern` is the command in `pattern build` and a placeholder in `tool build pattern`, and only the position separates them.

func Parses

func Parses(s string) bool

Parses reports whether a string is something a bash parser accepts. It replaces a quote counter that called `echo "it's fine"` unbalanced and `echo "a" "b` balanced, being wrong in both directions.

func Words

func Words(cmd string) ([]string, bool)

Words returns the words of the first simple command in a line, with quoting resolved, and reports whether the line parsed. It is the honest replacement for strings.Fields: a quoted argument comes back as one word.

Types

type Cmd

type Cmd struct {
	// Assigns are the NAME=value prefixes attached to this command.
	Assigns []string
	// Words are the command and its arguments, one word per argument, with
	// quoting resolved. A word whose value depends on an expansion keeps the
	// expansion's source text, since its real value is unknown until it runs.
	Words []string
	// Expanded marks a command holding a substitution or expansion whose
	// value kibble cannot know without running it.
	Expanded bool
}

Cmd is one simple command from a documented line, with its words as the shell would split them.

func (Cmd) Arg

func (c Cmd) Arg(n int) string

Arg returns the nth argument after the program name, or empty when the command has no such argument.

func (Cmd) Name

func (c Cmd) Name() string

Name returns the command's program name, or empty when it has none.

type Line

type Line struct {
	// Cmds are the simple commands the line runs, in source order, including
	// those inside pipelines, lists, subshells, and compound statements.
	Cmds []Cmd
	// Structured marks a line that is more than one simple command: a
	// pipeline, a list, a redirect, a subshell, a loop, or a conditional.
	Structured bool
	// StateChanging marks a line whose effect is on the shell itself, so
	// running it anywhere but the session's own shell would lose that effect.
	// A bare assignment, a cd, an export, a source, and their relatives all
	// qualify, wherever they appear in the line.
	StateChanging bool
	// Heredoc marks a line carrying a here-document body.
	Heredoc bool
	// Background marks a line the document itself puts in the background.
	Background bool
}

Line is a parsed documented line.

func Parse

func Parse(cmd string) (Line, bool)

Parse Parses one documented logical line. The second result is false when the line is not something a bash parser accepts, which is a fact about the line worth reporting rather than a reason to fall back to splitting on spaces.

Jump to

Keyboard shortcuts

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