cmdline

package
v0.0.0-...-4cab204 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const ErrUsage = ErrExitCode(2)

ErrUsage indicates an error in command usage; e.g. unknown flags, subcommands or args. It corresponds to exit code 2.

Variables

This section is empty.

Functions

func AddEnvToContext

func AddEnvToContext(ctx context.Context, env *Env) context.Context

AddEnvToContext returns a new Context with the `env` added to it.

func ExitCode

func ExitCode(err error, w io.Writer) subcommands.ExitStatus

ExitCode returns the exit code corresponding to err.

0:    if err == nil
code: if err is ErrExitCode(code)
1:    all other errors

Writes the error message for "all other errors" to w, if w is non-nil.

func Main

func Main(env *Env, commander *subcommands.Commander) subcommands.ExitStatus

Main implements the main function for the given commander.

Types

type Env

type Env struct {
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	Vars   map[string]string // Environment variables
	Timer  *timing.Timer

	// WorkingDir overrides the process working directory when resolving paths.
	WorkingDir string

	// Usage is a function that prints usage information to w.  Typically set by
	// calls to Main or Parse to print usage of the leaf command.
	Usage func(ctx context.Context, w io.Writer)

	// Comamnd name
	CommandName string
	// command flags
	CommandFlags map[string]string
}

Env represents the environment for command parsing and running. Typically EnvFromOS is used to produce a default environment. The environment may be explicitly set for finer control; e.g. in tests.

func EnvFromContext

func EnvFromContext(ctx context.Context) *Env

EnvFromContext retrieves the Env from the given Context.

func EnvFromOS

func EnvFromOS() *Env

EnvFromOS returns a new environment based on the operating system.

func (*Env) TimerPop

func (e *Env) TimerPop()

TimerPop calls e.Timer.Pop(), only if the Timer is non-nil.

func (*Env) TimerPush

func (e *Env) TimerPush(name string)

TimerPush calls e.Timer.Push(name), only if the Timer is non-nil.

func (*Env) UsageErrorf

func (e *Env) UsageErrorf(format string, args ...any) error

UsageErrorf prints the error message represented by the printf-style format and args, followed by the output of the Usage function. Returns ErrUsage to make it easy to use from within the Runner.Run function.

type ErrExitCode

type ErrExitCode int

ErrExitCode may be returned by Runner.Run to cause the program to exit with a specific error code.

func (ErrExitCode) Error

func (x ErrExitCode) Error() string

Error implements the error interface method.

Jump to

Keyboard shortcuts

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