Documentation
¶
Index ¶
- func ArgWords(cmd string) ([]string, bool)
- func ArgWordsOrFields(cmd string) []string
- func FirstWord(cmd string) string
- func HasPipe(cmd string) bool
- func IsSubcommandName(s string) bool
- func MatchesWords(line, match string) bool
- func Operands(cmd string) []string
- func Parses(s string) bool
- func SetupOnly(cmd string) bool
- func Words(cmd string) ([]string, bool)
- func WordsOrFields(cmd string) []string
- func Writes(cmd string) []string
- type Cmd
- type Line
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArgWords ¶
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
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
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 ¶
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
IsSubcommandName reports whether a word is shaped like a subcommand.
func MatchesWords ¶
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 ¶
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 ¶
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 SetupOnly ¶ added in v1.3.0
SetupOnly reports whether a line does nothing but arrange the shell or write files: a cd, an export, a mkdir, an assignment, or a heredoc or echo redirected into a file. Such a line passing is not a documented claim settled, since it would pass whatever the tool did. A line holding a command or process substitution runs something else and is never setup, and neither is a line that does not parse.
func Words ¶
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
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, in an argument or in an
// assignment prefixing the command.
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
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.
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.