shell

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 4 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

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 ArgWordsOrFields added in v0.0.5

func ArgWordsOrFields(cmd string) []string

ArgWordsOrFields returns a line's words, falling back to whitespace splitting only when the line does not parse. A line reaching here has already run, so it came from a document kibble could read; the fallback exists so a parse kibble did not anticipate degrades to the old answer rather than to no answer.

func FirstWord added in v0.0.5

func FirstWord(cmd string) string

FirstWord returns the program a line runs, or empty when it runs nothing. It never panics on a blank line, which indexing the split did.

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 IsSubcommandName added in v0.0.5

func IsSubcommandName(s string) bool

IsSubcommandName reports whether a word is shaped like a subcommand.

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.

func WordsOrFields added in v0.0.5

func WordsOrFields(cmd string) []string

WordsOrFields returns the words of a line's first command, the program name first, falling back to whitespace splitting only when the line does not parse. Planner rules that index by position need this rather than ArgWordsOrFields: a rule asking what the line runs, or reading its second argument, means the first command's words and not every word in a pipeline.

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 Invoked added in v0.23.0

func Invoked(c Cmd) Cmd

Invoked returns the command a simple command actually runs, after stepping through wrappers such as sudo, env and xargs. When a wrapper only reports on a command, carries it inside a single string, or names no command at all, the result has no words, since nothing kibble can name runs.

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