Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Command ¶
type Command struct {
// Name is the command name token (e.g., "mod", "init", "tidy")
Name string
// Usage is the usage line after the command path, e.g. "[-v] [path]".
Usage string
// Short is a one-line summary. Long is extended help (optional).
Short string
Long string
// Flags are parsed for this specific command before invoking run.
Flags *flag.FlagSet
// Run executes command logic. args are the remaining, non-flag args.
// Return *UsageError for bad usage; any other error prints and returns exit code 1.
Run func(ctx *Context, args []string) error
// Sub holds nested subcommands (e.g., "go mod" has children "init", "tidy")
Sub []*Command
// contains filtered or unexported fields
}
Command is a subcommand, mirroring cmd/go's style: a FlagSet, short/long help, optional children, and a Run func. Only the fields below are needed for a minimal, testable core.
func NewCommand ¶
func (*Command) Exec ¶
Exec dispatches args to subcommands, parses flags, and calls Run. It mirrors how cmd/go walks the command tree: first arg selects a child; if none, the current command's flags parse and Run executes.
func (*Command) PrintUsage ¶
PrintUsage writes a concise usage block for c.
type Context ¶
type Context struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// Env returns the value of an environment variable (nil-safe when not set)
Env func(key string) string
}
Context carries I/O and environment access. Keep it tiny and swappable in tests.
type UsageError ¶
type UsageError struct {
Err error
}
UsageError indicates "you used the command wrong" (prints usage + exit code 2).
func Usagef ¶
func Usagef(format string, a ...any) *UsageError
func (*UsageError) Error ¶
func (e *UsageError) Error() string
Click to show internal directories.
Click to hide internal directories.