cmdos

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MPL-2.0 Imports: 11 Imported by: 0

README

cmdos

Go Documentation

A flag and subcommand library

Installation

go get codeberg.org/roble/cmdos

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExitOK = &ExitError{}

ExitOK is used to signal that after running PreRun, the application should exit instead of continuing to execute Run.

Functions

func FlagValueImplicit

func FlagValueImplicit(v string) flagValueSpec

FlagValueImplicit signals that if a flag is set, the flag value may be explicitly set. If the flag's value isn't set, the given implicit value should be used.

func FlagValueNone

func FlagValueNone() flagValueSpec

FlagValueNone signals that this flag cannot have a value set.

func FlagValueRequired

func FlagValueRequired() flagValueSpec

FlagValueRequired signals that this flag must explicitly set a value.

func WriteHelp

func WriteHelp(w io.Writer, c Command) error

WriteHelp writes a command's help text to w.

Types

type Command

type Command interface {
	CommandSpec() CommandSpec
	ValueSpecs() []ValueSpec
	Run(*Values) error
}

Command is an instance of a command or subcommand to run.

type CommandSpec

type CommandSpec struct {
	Name        string
	Args        []string
	Summary     string
	Description string

	Subcommands []Command
}

CommandSpec contains info about a Command.

type CompletionsCommand added in v0.2.0

type CompletionsCommand struct{}

CompletionsCommand is a command that generates a Bash script for triggering shell command completions.

func (*CompletionsCommand) CommandSpec added in v0.2.0

func (c *CompletionsCommand) CommandSpec() CommandSpec

CommandSpec returns command details about the completions command.

func (*CompletionsCommand) Run added in v0.2.0

func (c *CompletionsCommand) Run(vs *Values) error

Run returns a script for triggering shell completions.

func (*CompletionsCommand) ValueSpecs added in v0.2.0

func (c *CompletionsCommand) ValueSpecs() []ValueSpec

CommandSpec returns value details about the completions command.

type ExitError

type ExitError struct {
	Code int
	Err  error
}

ExitError can be used to communicate an exit code back to the caller.

func (*ExitError) Error

func (e *ExitError) Error() string

type Runner

type Runner struct {
	Args []string
	Env  []string

	// PreRun is a function that gets run before a Command's Run function gets
	// called.
	PreRun func(Command, *Values) error
}

Runner runs Commands.

func (*Runner) Run

func (r *Runner) Run(c Command) error

Run takes a Command tree and uses Args to find the correct Command to run.

type ValueSpec

type ValueSpec struct {
	// ID is an ID for this value. If this isn't explicitly set, the longest
	// flag name will be used.
	ID          string
	Description string

	// Flags is a list of flags this value should read from.
	Flags []string
	// FlagValue sets the style of values Flags can have.
	FlagValue flagValueSpec

	// Env is a list of environment variables this value should read from.
	Env []string

	// Default is the default value this value has.
	Default string

	// Group is a group name that this value belongs to. Groups are mutually
	// exclusive.
	Group string
}

ValueSpec contains information about a specific value.

type Values

type Values struct {
	// contains filtered or unexported fields
}

Values provides access to all values collected for the Command.

func (*Values) Args

func (vs *Values) Args() []string

Args is a list of arguments passed to the current (sub)command.

func (*Values) CommandLine added in v0.2.0

func (vs *Values) CommandLine() []string

CommandLine is the full argument vector for this executable.

func (*Values) Decode

func (vs *Values) Decode(dst any) (err error)

Decode unmarshals flag values into a dst struct. The dst struct should tag its fields. The tag value should match the ValueSpec.ID field.

Help bool   `cmdos:"help"`
Addr string `cmdos:"addr"`

Jump to

Keyboard shortcuts

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