cmdutil

package
v0.4.13 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const ErrorCodeCmd string = "CMD_ERROR"

Variables

This section is empty.

Functions

func MergeArgs added in v0.4.13

func MergeArgs(sets ...[]string) []string

This function merges argument sets left to right, so a later set overrides an earlier one. A token longer than one character and starting with "-" is a flag; following non-flag tokens are its values. A repeated flag keeps its first-seen position and takes its last-seen value list, so MergeArgs([]string{"-a", "-b", "2", "-b", "3"}) returns ["-a", "-b", "3"].

The --flag=value form splits on the first "=" and merges with the space form of the same flag; the emitted form follows the last-seen occurrence. Bare "-" and tokens with no flag open are positionals, passed through in place. A flag does not stay open across a set boundary.

The result is newly allocated and never aliases an input. It has a time complexity of O(n) in the total number of tokens.

This function infers flag arity positionally, because an argument list does not say which flags take a value. Two consequences:

  • Repeatable flags collapse: ["-v", "-v"] returns ["-v"], and ["--header", "A", "--header", "B"] returns ["--header", "B"]. Do not use MergeArgs for tools whose flags accumulate.
  • A trailing positional is absorbed as the preceding flag's value: ["--verbose", "file.txt"] parses file.txt as --verbose's value. There is no "--" terminator to guard against this.

Types

type Command

type Command interface {
	Output() string
	Error() string
	Start() error
	Wait() error
	Print(fns ...PrintFunc)
	String() string
	ExitCode() int
}

func New

func New(bin string, args []string, opts ...Option) Command

type Option

type Option func(*cmd)

func Async

func Async() Option

func WithCancel added in v0.3.13

func WithCancel(fn func(*os.Process) error) Option

func WithContext

func WithContext(ctx context.Context) Option

func WithDir added in v0.3.13

func WithDir(dir string) Option

func WithEnv

func WithEnv(envs ...string) Option

func WithInput

func WithInput(inputs ...io.Reader) Option

func WithMaxBuffer added in v0.3.13

func WithMaxBuffer(n int) Option

func WithStderr added in v0.3.13

func WithStderr(w io.Writer) Option

func WithStdout added in v0.3.13

func WithStdout(w io.Writer) Option

func WithWaitDelay added in v0.3.13

func WithWaitDelay(d time.Duration) Option

type PrintFunc

type PrintFunc func(line string)

Jump to

Keyboard shortcuts

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