opt

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: MIT Imports: 6 Imported by: 2 modules, 2 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

opt

CLI arguments and tool commands.

What

This package parses a tagged struct then sets variables and/or executes commands specified by the struct.

How

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUsage is really no error, but signals that help text should be printed.
	ErrUsage = errors.New("unknown options")
	// ErrNoCommand is a warning that no command was supplied.
	ErrNoCommand = errors.New("no command specified")
)

Functions

func HandleCommands added in v0.2.0

func HandleCommands(options interface{}) error

HandleCommands is a shortcut for handling commands and outputting usage test, returning any serious errors.

Types

type Args

type Args struct {
	Program string

	Remaining []string
	// contains filtered or unexported fields
}

Args gets options and commands parsed into it.

func Parse

func Parse(data any) *Args

Parse the command line for arguments and tool commands.

func (*Args) Parse

func (a *Args) Parse(data any, in []string, parent string)

Parse an option structure and slice of arguments.

func (*Args) RunCommand

func (a *Args) RunCommand(all bool) error

RunCommand and recurse.

func (*Args) SetChoicesLong

func (a *Args) SetChoicesLong(name string, list []string)

SetChoicesLong sets the selectable options based on the long option name.

func (*Args) SetChoicesShort

func (a *Args) SetChoicesShort(name string, list []string)

SetChoicesShort sets the selectable options based on the short option name.

func (*Args) Usage

func (a *Args) Usage()

Usage printout.

type DefaultHelp

type DefaultHelp struct {
	Help bool `short:"h" help:"Show this help."`
}

DefaultHelp can be embedded in your options struct to save some typing.

type Flag

type Flag struct {
	Name        string
	Help        string
	Short       string
	Long        string
	Group       string
	Placeholder string
	CommandName string
	Default     string
	Choices     []string
	Aliases     []string
	Args        *Args

	IsCommand bool
	IsSlice   bool
	IsMap     bool
	Required  bool
	// contains filtered or unexported fields
}

Flag or command option.

func (*Flag) AddChoice

func (f *Flag) AddChoice(c string)

AddChoice to list. Makes the choice lowercase and trims leading and trailing spaces.

func (*Flag) SetChoices

func (f *Flag) SetChoices(list []string)

SetChoices to a new list.

func (*Flag) UsageString

func (f *Flag) UsageString() (string, string)

UsageString returns the help text.

type Runner

type Runner interface {
	Run(args []string) error
}

Runner is the interface for tool commands to conform to.

Jump to

Keyboard shortcuts

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