cli

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewValSet

func NewValSet(allowed ...string) func(value string) error

NewValSet returns a validation function that accepts a set of strings.

func ValInt

func ValInt(value string) error

ValInt is a validation function that returns an error if the value is not an integer.

func ValString

func ValString(value string) error

ValString is a validation function that always returns nil, accepting any string.

Types

type Cli

type Cli struct {
	Logger

	// Description describes the application.
	Description string

	// The syntax description of the command. It appears in Usage after the
	// name of the program (os.Args[0]). If not specified, "[OPTIONS]..."
	// will be used instead.
	Syntax string

	// Flag is a set of flags to parse.
	Flags []*Flag

	// Args is a list of leftover arguments that were not parsed.
	Args []string

	// Sub contains sub-commands.
	Sub map[string]*Cli

	// Super contains the string of commands used to invoke this Cli.
	// For example, a command invoked using
	//   <program> foo bar
	// Would have Super set to
	//   "foo bar"
	// This is automatically filled out by Cli.AddSub.
	Super string
}

Cli represents a command line interface. It is not tied to os.Args, and it is possible to create multiple Cli's to have so many sub-commands in one program.

func New

func New(description string, flags ...*Flag) *Cli

New creates a new Cli from a command description and a set of flags. These flags should be created as variables before passing them to New(), so that their values can be extracted after Parse is called.

func (*Cli) AddSub

func (this *Cli) AddSub(name string, sub *Cli)

AddSub adds a sub-command to this command. It automatically fills out the sub-command's Super field, and alters the prefix of its Logger to match.

func (*Cli) LongFlag

func (this *Cli) LongFlag(long string) (*Flag, bool)

LongFlag searches for and returns the flag with the given long form. If it was found, it returns true. If it was not found, it returns nil, false.

func (*Cli) Parse

func (this *Cli) Parse(args []string) (*Cli, error)

Parse parses the given set of command line arguments according to the flags defined within the Cli. The first element of the argument slice is always dropped, because it is assumed that it just contains the command name. If the second element matches up with the name of a sub-command, parsing is deferred to that sub-command instead. The sub-command will be given all but the first element of args. This method will return the address of the command that did the parsing.

func (*Cli) ParseOrExit

func (this *Cli) ParseOrExit(args []string) *Cli

ParseOrExit is like Parse, but if an error occurs while parsing the argument list, it prints out Usage and exits with error status 2.

func (*Cli) ShortFlag

func (this *Cli) ShortFlag(short rune) (*Flag, bool)

ShortFlag searches for and returns the flag with the given short form. If it was found, it returns true. If it was not found, it returns nil, false.

func (*Cli) Usage

func (this *Cli) Usage()

Usage prints out usage/help information.

type Flag

type Flag struct {
	Short rune   // The short form of the flag (-l)
	Long  string // Long form of the flag (--long-form)
	Help  string // Help text to display by the flag

	// Validate is an optional function that is called when an input is
	// passed to the flag. It checks whether or not the input is valid, and
	// returns an error if it isn't. If this function is nil, the flag is
	// assumed to have no input.
	Validate func(string) error

	// Value contains the input given to the flag, and is filled out when
	// the flags are parsed. If this is a non-input flag (if Validate is
	// nil), this will be set to true. If the flag was not specified, Value
	// will be unchanged.
	Value string

	// Found is an optional function that is called each time the flag is
	// found, and has a valid argument (if applicable). The value is passed
	// to the function, as well as the command that it was found in.
	Found func(*Cli, string)
}

Flag is a command line option.

func NewFlag

func NewFlag(short rune, long string, help string) *Flag

NewFlag creates a new flag that does not take in a value.

func NewHelp

func NewHelp() *Flag

NewHelp creates a new help flag activated by --help or -h. It shows help information for the command that it is a part of, and causes the program to exit with a status of 2.

func NewInputFlag

func NewInputFlag(short rune, long string, help string, defaul string, validate func(string) error) *Flag

NewInputFlag creates a new flag that does take in a value. This function will panic if the given validation function is nil.

func (*Flag) String

func (this *Flag) String() string

String returns --<LongForm> if specified, and if not returns -<ShortForm>

type Logger

type Logger struct {
	// Writer specifies the writer to output to. If it is nil, the Logger
	// will output to os.Stderr. If you wish to silence the output, set it
	// to io.Discard.
	io.Writer

	// Debug determines whether or not debug messages will be logged.
	Debug bool

	// Prefix is printed before all messages. If this is an empty string,
	// os.Args[0] will be used.
	Prefix string
}

Logger prints messages to an output writer.

func (*Logger) Debugln

func (this *Logger) Debugln(v ...any)

Debugln logs debugging information. It will only print the message if the Debug field is set to true.

func (*Logger) Errorln

func (this *Logger) Errorln(v ...any)

Errorln logs an error message.

func (*Logger) Println

func (this *Logger) Println(v ...any)

Println logs a normal message.

func (*Logger) Warnln

func (this *Logger) Warnln(v ...any)

Warnln logs a warning.

func (*Logger) Write

func (this *Logger) Write(data []byte) (n int, err error)

Write writes to the Logger's writer. If it is nil, it writes to os.Stderr.

Jump to

Keyboard shortcuts

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