command

package
v0.8.0-alpha.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidBundleCommandPair is returned by FindCommandEntry when the
	// command entry string doesn't look like  "command" or "bundle:command".
	ErrInvalidBundleCommandPair = errors.New("invalid bundle:comand pair")
)

Functions

func SplitCommand

func SplitCommand(name string) (bundle, command string, err error)

SplitCommand accepts a string in the style of "bundle:command" or "command" and returns the bundle and command as a pair of strings. If there's no indicated bundle, the bundle string (the first string) will be empty. If there's more than one colon, an error will be returned.

func Tokenize

func Tokenize(input string) ([]string, error)

Tokenize takes an input string and splits it into tokens. Any control character sequences ("\n", "\t", etc), pass through in their original form. Examples:

echo -n foo bar -> {"echo", "-n", "foo", "bar"}
echo -n "foo bar" -> {"echo", "-n", "foo bar"}
echo "What's" "\"this\"?" -> {"echo", "What's", "\"this\"?"}

Types

type Command

type Command struct {
	Bundle     string
	Command    string
	Options    map[string]CommandOption
	Parameters CommandParameters
}

Command represents a command typed in by a user. It is typically generated by the Parse function.

func Parse

func Parse(tokens []string, options ...ParseOption) (Command, error)

Parse accepts a slice of token strings and constructs a Command value. Its behavior may be modified by passing one or more ParseOptions.

func TokenizeAndParse

func TokenizeAndParse(str string, options ...ParseOption) (Command, error)

TokenizeAndParse is a helper function that combines the Tokenize and Parse functions.

func (Command) OptionsValues

func (c Command) OptionsValues() map[string]types.Value

type CommandOption

type CommandOption struct {
	Name  string
	Value types.Value
}

CommandOption represents a command option or flag, and its string value (if any).

type CommandParameters

type CommandParameters []types.Value

func (CommandParameters) String

func (c CommandParameters) String() string

type ParseOption

type ParseOption func(*parseOptions)

func ParseAgnosticDashes

func ParseAgnosticDashes(agnostic bool) ParseOption

ParseAgnosticDashes modifies how dashes are interpreted. If true, double and single dashes are treated the same. If false (default), then double-dashed options are "long" and single-dashed options are "short".

func ParseAssumeOptionArguments

func ParseAssumeOptionArguments(assume bool) ParseOption

ParseAssumeOptionArguments changes the assumption about whether an option not specified using ParseOptionHasArgument should be treated as having an option. If false (default), unknown options are assumed to have no argument.

func ParseOptionAlias

func ParseOptionAlias(alias, name string) ParseOption

ParseOptionAlias allows option aliases to be set, most often "short options" to "long options". All references to "alias" are treated as "name".

func ParseOptionHasArgument

func ParseOptionHasArgument(option string, hasArg bool) ParseOption

ParseOptionHasArgument allows specific options to be specified as expecting an option (or not). Options not specified are treated according to ParseAssumeOptionArguments.

type TokenizeError

type TokenizeError struct {
	Text     string
	Position int
}

func (TokenizeError) Error

func (e TokenizeError) Error() string

Jump to

Keyboard shortcuts

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