cli

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cli is the small command framework behind the ypy binary: an explicit list of subcommands, a usage printer and a flag parser that accepts flags and positional arguments in any order.

There is no global registry: cmd/ypy builds the App value and passes it the commands, so what the binary can do is readable in one place.

Index

Constants

View Source
const (
	ExitOK       = 0 // nothing to report
	ExitFailure  = 1 // the command ran and found a problem (findings, drift, over budget)
	ExitUsage    = 2 // bad flags, unreadable input
	ExitInternal = 70
)

Exit codes shared by every subcommand.

Variables

This section is empty.

Functions

func ParseInterspersed

func ParseInterspersed(fs *flag.FlagSet, args []string) ([]string, error)

ParseInterspersed parses args with fs, letting flags and positional arguments appear in any order (the standard library stops at the first positional argument). A "--" ends the flags.

Types

type App

type App struct {
	Name     string
	Version  string
	Commands []Command
}

App is the binary: a name, a version and its subcommands.

func (App) Run

func (a App) Run(ctx context.Context, args []string, io IO) int

Run dispatches args (without the program name) to a subcommand.

type Command

type Command struct {
	Name    string
	Summary string
	// Run executes the command with the arguments that follow its name and
	// returns the process exit code.
	Run func(ctx context.Context, args []string, io IO) int
}

Command is one subcommand of the binary.

type IO

type IO struct {
	Out   io.Writer
	Err   io.Writer
	In    io.Reader
	Env   func(key string) string
	IsTTY bool
	// Dir is the working directory used to resolve relative paths; empty means
	// the process working directory.
	Dir string
}

IO is what a command may touch: it never reads os.Stdout or os.Getenv directly, so tests run commands in memory.

func OS

func OS() IO

OS returns the IO of the running process.

type ListValue

type ListValue struct {
	Items *[]string
}

ListValue is a flag.Value for a repeatable, comma separated option.

func (ListValue) Set

func (l ListValue) Set(v string) error

Set implements flag.Value.

func (ListValue) String

func (l ListValue) String() string

String implements flag.Value.

Jump to

Keyboard shortcuts

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