clui

package module
v0.0.0-...-5d27ecb Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: Apache-2.0 Imports: 16 Imported by: 2

README

clui

Command Line UI

Documentation

Index

Constants

View Source
const TemplateComplete = `
{{- range $name := .}}
{{ $name }}
{{- end}}
`

TemplateComplete describes a template for rendering autocomplete commands.

View Source
const TemplateFlags = `
{{.Name}}	{{.Usage}} (defaults: "{{.Defaults}}")
`

TemplateFlags describes a template for rendering flags in help.

View Source
const TemplatePlaceHolder = `
Selection of commands that are nested under this one.
`

TemplatePlaceHolder describes a template for rendering placeholder text.

View Source
const TemplateVersion = `
Client version: {{ .Version }}
`

TemplateVersion describes a template for rendering version.

Variables

This section is empty.

Functions

func FindChildren

func FindChildren(commands *group.Group, prefix string, includeSubKeys bool) (map[string]Command, error)

FindChildren returns the sub commands. This will only contain immediate sub commands.

Types

type AutoCompleter

type AutoCompleter interface {
	// Complete a command from completion line in environment variable,
	// and print out the complete options.
	// Returns success if the completion ran or if the cli matched
	// any of the given flags, false otherwise
	Complete(string) ([]string, bool)

	// Install a command into the host using the Installer.
	// Returns an error if there is an error whilst installing.
	Install(string) error

	// Uninstall the command from the host using the Installer.
	// Returns an error if there is an error whilst it's uninstalling.
	Uninstall(string) error
}

AutoCompleter is an interface to be implemented to perform the autocomplete installation and un-installation with a CLI.

This interface is not exported because it only exists for unit tests to be able to test that the installation is called properly.

type CLI

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

CLI contains the state necessary to run commands and parse the command line arguments

CLI also supports nested subCommands, such as "cli foo bar". To use nested subCommands, the key in the Commands mapping below contains the full subCommand. In this example, it would be "foo bar"

func New

func New(name, version, header string, options ...CLIOption) *CLI

New returns a new CLI instance with sensible default.

func (*CLI) Add

func (c *CLI) Add(key string, cmdFn CommandFn) error

Add inserts a new command to the CLI.

func (*CLI) Run

func (c *CLI) Run(args []string) (Errno, error)

Run runs the actual CLI bases on the arguments given.

type CLIOption

type CLIOption func(CLIOptions)

CLIOption captures a tweak that can be applied to the CLI.

func OptionAutoCompleter

func OptionAutoCompleter(i AutoCompleter) CLIOption

OptionAutoCompleter allows the setting a AutoCompleter option to configure the cli.

func OptionFileSystem

func OptionFileSystem(i fsys.FileSystem) CLIOption

OptionFileSystem allows the setting a FileSystem option to configure the cli.

func OptionHelpFunc

func OptionHelpFunc(i help.Func) CLIOption

OptionHelpFunc allows the setting a HelpFunc option to configure the cli.

func OptionUI

func OptionUI(i UI) CLIOption

OptionUI allows the setting a UI option to configure the cli.

type CLIOptions

type CLIOptions interface {
	SetHelpFunc(help.Func)
	SetAutoCompleter(AutoCompleter)
	SetUI(UI)
	SetFileSystem(fsys.FileSystem)
}

CLIOptions represents a way to set optional values to a CLI option. The CLIOptions shows what options are available to change.

type Command

type Command interface {

	// Flags returns the FlagSet associated with the command. All the flags are
	// parsed before running the command.
	FlagSet() *flagset.FlagSet

	// Usages returns various usages that can be used for the command.
	Usages() []string

	// Help should return a long-form help text that includes the command-line
	// usage. A brief few sentences explaining the function of the command, and
	// the complete list of flags the command accepts.
	Help() string

	// Synopsis should return a one-line, short synopsis of the command.
	// This should be short (50 characters of less ideally).
	Synopsis() string

	// Init is called with all the args required to run a command.
	// This is separated from Run, to allow the preperation of a command, before
	// it's run.
	Init([]string, commands.CommandContext) error

	// Run should run the actual command with the given CLI instance and
	// command-line arguments. It should return the exit status when it is
	// finished.
	//
	// There are a handful of special exit codes that can return documented
	// behavioral changes.
	Run(*task.Group)
}

Command is a runnable sub-command of CLI.

type CommandFn

type CommandFn func(UI) Command

CommandFn defines a function for constructing a command.

type Errno

type Errno int

Errno represents a error constants that can be reutrned from the CLI

const (
	// EOK is non-standard representation of a success
	EOK Errno = 0

	// EPerm represents an operation not permitted
	EPerm Errno = 1

	// EKeyExpired is outside of POSIX 1, represents unknown error.
	EKeyExpired Errno = 127
)

func (Errno) Code

func (e Errno) Code() int

Code converts the Errno back into an int when type inference fails.

type GlobalArgs

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

GlobalArgs is used to construct the arguments used for the CLI. The global arguments are then passed to the command once found, without the global flags.

func NewGlobalArgs

func NewGlobalArgs(commands *group.Group) *GlobalArgs

NewGlobalArgs creates a new GlobalArgs type for processing arguments passed to the cli.

func (*GlobalArgs) CommandFlags

func (a *GlobalArgs) CommandFlags() []string

CommandFlags returns the command arguments.

func (*GlobalArgs) Debug

func (a *GlobalArgs) Debug() bool

Debug returns if the operator has passed the debug flag.

func (*GlobalArgs) DevMode

func (a *GlobalArgs) DevMode() bool

DevMode returns if the operator has passed the devMode flag.

func (*GlobalArgs) Help

func (a *GlobalArgs) Help() bool

Help returns if the operator has passed the help flag.

func (*GlobalArgs) Process

func (a *GlobalArgs) Process(args []string) error

Process consumes the arguments and correctly separates them between global flags and arguments and command flags and arguments.

func (*GlobalArgs) RequiresInstall

func (a *GlobalArgs) RequiresInstall() bool

RequiresInstall returns if the operator has passed the requires install flag.

func (*GlobalArgs) RequiresNoColor

func (a *GlobalArgs) RequiresNoColor() bool

RequiresNoColor returns if the operator has passed the no color output flag.

func (*GlobalArgs) RequiresNoSubKeys

func (a *GlobalArgs) RequiresNoSubKeys() bool

RequiresNoSubKeys returns if the help should include subkeys.

func (*GlobalArgs) RequiresUninstall

func (a *GlobalArgs) RequiresUninstall() bool

RequiresUninstall returns if the operator has passed the requires uninstall flag.

func (*GlobalArgs) SubCommand

func (a *GlobalArgs) SubCommand() string

SubCommand returns the sub command name.

func (*GlobalArgs) SubCommandArgs

func (a *GlobalArgs) SubCommandArgs() []string

SubCommandArgs returns the sub command arguments.

func (*GlobalArgs) SubCommandFlags

func (a *GlobalArgs) SubCommandFlags() []string

SubCommandFlags returns the sub command flags.

func (*GlobalArgs) Version

func (a *GlobalArgs) Version() bool

Version returns if the operator has passed the version flag.

type UI

type UI interface {
	// Ask asks the user for input using the given query. The response is
	// returned as the given string, or an error.
	Ask(string) (string, error)

	// AskSecret asks the user for input using the given query, but does not echo
	// the keystrokes to the terminal.
	AskSecret(string) (string, error)

	// Output is called for normal standard output.
	Output(*ui.Template, interface{}) error

	// Info is called for information related to the previous output.
	// In general this may be the exact same as Output, but this gives
	// UI implementors some flexibility with output formats.
	Info(string)

	// Error is used for any error messages that might appear on standard
	// error.
	Error(string)
}

UI is an interface for interacting with the terminal, or "interface" of a CLI.

Directories

Path Synopsis
ui
ask

Jump to

Keyboard shortcuts

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