command

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package command runs physa's commands on Cobra. Cobra holds the tree of grouped commands, routes a command line to the command it names and parses every command's flags from its declaration: a command declares its flags once, on its own Cobra command, and its job gets only the positionals. Help prints the command's usage lines, its summary and its subcommands. --help anywhere after a command's name prints its help to stdout; -h is not a flag. A bad line, a bad flag included, prints that command's help to stderr and exits 2, with no error text of its own. Names match exactly: there are no aliases, no short flags, no prefix matching and no suggestions. Shell completion comes from the declarations through a hidden completion command. Drift ties every command's usage lines to its flags.

Index

Constants

View Source
const BadUsage = -1

BadUsage is what a Run returns for arguments it cannot parse. The command's help is then printed to stderr and the exit code is 2.

Variables

This section is empty.

Functions

func Declare

func Declare(c *cobra.Command, flags func(fs *pflag.FlagSet)) *cobra.Command

Declare gives c, built by New, its flags: flags declares them on fs, and pflag parses them, so c's job gets only its positionals. flags runs again before every command line Execute runs, so each line starts from the defaults; a value it does not build afresh implements Resetter. A bad flag, a missing or invalid value, a shorthand such as -h and an empty string value print c's help to stderr and exit 2. flags must not declare help.

func Drift

func Drift(root *cobra.Command) []string

Drift returns one line per mismatch between a command's usage lines and its flags, for every command under root: a visible flag its usage lines don't name, or a --flag they name that it doesn't declare. A usage line that names one of the command's subcommands belongs to that subcommand.

func Execute

func Execute(root *cobra.Command, args []string, stdout, stderr io.Writer) int

Execute runs one command line against root and returns the process exit code: the command's own, 0 for --help, 2 for a bad line. The root's own help subcommand, if it has one, is its help command; without one, help is a word like any other and Cobra adds none.

func Help

func Help(c *cobra.Command, w io.Writer)

Help prints c's usage lines, its summary and its visible subcommands in the order they were declared. The output depends only on the tree.

func New

func New(name, summary string, usage []string, run Run, subs ...*cobra.Command) *cobra.Command

New builds a command named name with its summary, its usage lines (each a full command line such as "physa map index [dir]"), the job run does (nil for a group with no bare job) and its subcommands, in order. It takes no flags until Declare gives it some, so any flag is a bad flag.

func Switch

func Switch(fs *pflag.FlagSet, name string, on *bool, usage string)

Switch declares --name on fs as a switch bound to on, off by default: it is on when named bare and takes no value, so --name=false and --name=1 are bad flags, as they were before Cobra parsed physa's flags.

func Words

func Words(c *cobra.Command) []string

Words returns the words typed after c's name on the line Execute is running, in order and unparsed, or nil for a command New did not build.

Types

type Resetter

type Resetter interface {
	Reset()
}

Resetter is a flag value that keeps state its declaration does not build afresh. Its Reset puts it back to its default before each command line.

type Run

type Run func(args []string, stdout, stderr io.Writer) int

Run does a command's job with the arguments left after its name and returns its exit code, or BadUsage.

Jump to

Keyboard shortcuts

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