cli

package
v0.69.5 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 17 Imported by: 1 module, 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.

README

CLI flags

Declare boolean switches with Kind: cli.FlagKindBool:

cli.Flag{Long: "worktrees", Short: "w", Kind: cli.FlagKindBool}

A bare boolean switch is always true and never consumes the next argument. Both clone --worktrees topic workspace and --worktrees clone topic workspace keep the command and positional arguments intact. Use --worktrees=false or -w=false for an explicit value. Absent boolean flags default to false unless Default specifies a different boolean value. Repeated switches use the last value. Invalid explicit values and defaults produce an error during blueprint creation.

Cli.Run uses command, group, and global flag definitions when parsing. Aliases can have different kinds in different commands. String and list flags retain value-taking behavior. -- ends flag parsing, and grouped short switches remain supported.

For parsing without running an application, use NewInputWithRouter(args, router, globalFlags...), then NewBlueprint(router, input, globalFlags...). For a flat set of flag definitions, use NewInputWithArgs(args, flags...). Calls without flag definitions preserve the existing string parsing behavior; built-in help switches are always boolean.

Positional argument validation

Set Cmd.ValidateArgs to check the selected command's positional arguments. NewBlueprint calls it after flag parsing and returns its error if validation fails. Cli.Run reports the error on stderr without starting the application. Help requests skip validation. Cmd.Arguments only controls help text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetArguments added in v0.63.7

func GetArguments(config cfg.Config) ([]string, error)

GetArguments returns the positional arguments from the application's cli.args configuration section.

func LogHandler added in v0.62.1

func LogHandler(channels ...string) logHandler

LogHandler creates a CLI log handler that writes debug output for the selected channels.

func Module added in v0.63.7

func Module[T any](fac func(ctx context.Context, config cfg.Config, logger log.Logger) (T, error), call func(m T) Handler) application.Option

Module builds a main application module from a typed dependency factory and command handler.

func UnmarshalFlags added in v0.63.7

func UnmarshalFlags[T any](config cfg.Config) (*T, error)

UnmarshalFlags unmarshals CLI flag values from the application's cli.flags configuration section.

func WithRunFunc added in v0.62.1

func WithRunFunc(f func(ctx context.Context, config cfg.Config, logger log.Logger) (kernelPkg.ModuleRunFunc, error)) kernelPkg.ModuleFactory

WithRunFunc adapts a module run function factory into a gosoline module factory.

Types

type Blueprint added in v0.62.1

type Blueprint struct {
	Cmd        []string
	Args       []string
	Flags      []BlueprintFlag
	AppOptions []application.Option
}

Blueprint describes the command, arguments, flags, and application options selected from parsed CLI input.

func NewBlueprint added in v0.62.1

func NewBlueprint(router *Router, input *Input, globalFlags ...Flag) (*Blueprint, error)

NewBlueprint resolves parsed input against a router and returns the command blueprint to execute.

type BlueprintFlag added in v0.62.1

type BlueprintFlag struct {
	Key        string
	CustomKey  string
	Value      any
	AppOptions []application.Option
}

BlueprintFlag contains the resolved value and configuration bindings for a matched CLI flag.

type Cli added in v0.62.1

type Cli struct {
	*Router
	// contains filtered or unexported fields
}

Cli wires command routing, option parsing, and gosoline application startup for command line programs.

func NewCli added in v0.62.1

func NewCli(options ...Option) *Cli

NewCli creates a CLI with the default router and applies the provided options when Run is called.

func (*Cli) Run added in v0.62.1

func (c *Cli) Run()

Run parses process arguments, resolves the selected command, and starts the configured gosoline application.

type Cmd added in v0.62.1

type Cmd struct {
	Name         string
	Description  string
	Arguments    CmdArguments
	Examples     []CmdExample
	Flags        []Flag
	AppOptions   []application.Option
	ValidateArgs func([]string) error
}

Cmd defines an executable command with help metadata, flags, and application options.

type CmdArguments added in v0.63.7

type CmdArguments string

CmdArguments identifies whether a CLI command accepts positional arguments.

const (
	// CmdArgumentsNone documents that a command does not accept positional arguments.
	CmdArgumentsNone CmdArguments = ""
	// CmdArgumentsSingle documents that a command accepts one positional argument.
	CmdArgumentsSingle CmdArguments = "single"
	// CmdArgumentsMultiple documents that a command accepts multiple positional arguments.
	CmdArgumentsMultiple CmdArguments = "multiple"
)

type CmdExample added in v0.63.7

type CmdExample struct {
	Description string
	Args        string
}

CmdExample describes a command example rendered in help output.

type Flag added in v0.62.1

type Flag struct {
	Short       string
	Long        string
	Kind        FlagKind
	CfgKey      string
	Default     string
	Description string
	AppOptions  []application.Option
}

Flag defines a supported CLI flag and how it maps into application configuration.

type FlagKind added in v0.63.7

type FlagKind string

FlagKind identifies how a CLI flag value is parsed.

const (
	// FlagKindBool parses a boolean switch; a bare switch is true and explicit values use =.
	FlagKindBool FlagKind = "bool"
	// FlagKindString parses a flag as a single string value where the last occurrence wins.
	FlagKindString FlagKind = "string"
	// FlagKindList parses every occurrence of a flag into a string slice.
	FlagKindList FlagKind = "list"
)

type Group added in v0.62.1

type Group struct {
	Name        string
	Description string
	Flags       []Flag
	AppOptions  []application.Option
	// contains filtered or unexported fields
}

Group defines a named command namespace with shared flags and application options.

type Handler added in v0.63.7

type Handler = kernel.ModuleRunFunc

Handler is the function executed by a CLI command module.

type Input added in v0.62.1

type Input struct {
	Arguments []string
	Flags     []InputFlag
	// contains filtered or unexported fields
}

Input contains the parsed command line arguments and flags.

func NewInput added in v0.62.1

func NewInput(flags ...Flag) (*Input, error)

NewInput parses the current process command line arguments.

func NewInputWithArgs added in v0.62.1

func NewInputWithArgs(args []string, flags ...Flag) (*Input, error)

NewInputWithArgs parses arguments using optional flag definitions. Boolean flags never consume the next argument; explicit boolean values must use =.

func NewInputWithRouter added in v0.69.3

func NewInputWithRouter(args []string, router *Router, globalFlags ...Flag) (*Input, error)

NewInputWithRouter parses arguments using the flags of the selected command, including inherited group and global flags. This permits boolean switches before command names and preserves aliases with different kinds across commands.

type InputFlag added in v0.63.7

type InputFlag struct {
	Name  string
	Value string
}

InputFlag is a parsed command line flag name and value pair.

type Option added in v0.62.1

type Option func(cli *Cli)

Option configures a Cli before it parses input and runs the selected command.

func WithAppOptions added in v0.62.1

func WithAppOptions(option ...application.Option) Option

WithAppOptions adds gosoline application options that apply to every command.

func WithFlag added in v0.62.1

func WithFlag(flag Flag) Option

WithFlag registers a global CLI flag available to every command.

func WithHelp added in v0.63.7

func WithHelp(name string, description string) Option

WithHelp configures top-level help text and registers the built-in help command.

func WithHelpLineLength added in v0.63.7

func WithHelpLineLength(length int) Option

WithHelpLineLength configures the maximum rendered help line length. Use a negative value to disable wrapping.

func WithVersion added in v0.62.1

func WithVersion(version string) Option

WithVersion registers a built-in version command that prints the provided version string.

type Router added in v0.62.1

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

Router stores the command and group hierarchy used to resolve CLI input.

func NewRouter added in v0.62.1

func NewRouter(parent *Router) *Router

NewRouter creates an empty router with an optional parent router for nested command groups.

func (*Router) Cmd added in v0.62.1

func (r *Router) Cmd(cmd Cmd) *Router

Cmd registers a command on the router.

func (*Router) DefaultCmd added in v0.62.1

func (r *Router) DefaultCmd(cmd Cmd) *Router

DefaultCmd sets the command used when no explicit command matches the input.

func (*Router) Group added in v0.62.1

func (r *Router) Group(group Group, subCommands ...Cmd) *Router

Group registers a command group and optional subcommands, returning the group's child router.

Jump to

Keyboard shortcuts

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