ankh

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 21 Imported by: 0

README

ankh

A Library for implementing command line interfaces in Go

Features

  • Eeasy subcommands based CLIs: app-cli foo, app-cli bar.

  • Support for nested subcommands such as app-cli foo bar

  • Optional support for default subcommands so app-cli does something other than error

  • Support for shell autocompletion of commands, flags and arguments with callbacks in Go. You don't need to write any shell code.

  • Automatic help generation for listing subcommands.

  • Automatic help flag recognition of -h, --help etc.

  • Automatic version flag recognition -, --version

  • Helpers for interacting with the terminal, such as outputing information, aksing for input etc. These are optional, you can always interact with the terminal however you choose.

  • Use of Go interfaces/types makes augmenting various parts of the library a piece of cake.

Example

Below is a simple example of running a CLI


package main 

import (
    "log" 
    "os" 

    "github.com/irononet/ankh"


    func main(){
        cli := ankh.NewCLI("app", "0.1.0") 
        cli.Args = os.Args[1:]
        cli.Commands = map[string]ankh.CommandFactory{
            "foo": fooCommand, 
            "bar": barCommand,
        }

        exitStatus, err := cli.Run()
        if err != nil{
            log.Println(err) 
        }

        os.Exit(exitStatus)
    }
)

Documentation

Index

Constants

View Source
const (
	// RunResultHelp is a value that can be returned from Run to signal
	// to the CLI to render the help output.
	RunResultHelp = -18511
)

Variables

View Source
var (
	UiColorNone    UiColor = UiColor{noColor, false}
	UiColorRed             = UiColor{int(color.FgHiRed), false}
	UiColorGreen           = UiColor{int(color.FgHiGreen), false}
	UiColorYellow          = UiColor{int(color.FgHiYellow), false}
	UiColorBlue            = UiColor{int(color.FgHiBlue), false}
	UiColorMagenta         = UiColor{int(color.FgHiMagenta), false}
	UiColorCyan            = UiColor{int(color.FgHiCyan), false}
)

A list of colors that are useful. These are all non-bolded by default.

Functions

This section is empty.

Types

type BasicUi

type BasicUi struct {
	Reader      io.Reader
	Writer      io.Writer
	ErrorWriter io.Writer
}

BasicUi is an implementation of Ui that just outputs to the given writer. This Ui is not threadsafe by default, but you can wrap it in a concurrentUi to make it safe.

func (*BasicUi) Ask

func (u *BasicUi) Ask(query string) (string, error)

func (*BasicUi) AskSecret

func (u *BasicUi) AskSecret(query string) (string, error)

func (*BasicUi) Error

func (u *BasicUi) Error(message string)

func (*BasicUi) Info

func (u *BasicUi) Info(message string)

func (*BasicUi) Output

func (u *BasicUi) Output(message string)

func (*BasicUi) Warn

func (u *BasicUi) Warn(message string)

type CLI

type CLI struct {
	// Args is the list of command-line arguments received excluding
	// the name of the app. For example, if the command "./cli foo bar"
	// was invoked, then Args should be []string{"foo", "bar"}
	Args []string

	// Commands is a mapping of subcommands names to a factory function
	// for creating that Command implementation. If there is a command
	// with a blank string "", then it will be used as the default command
	// if no subcommand is specified.
	//
	// If the key has a space in it, this will create a nested subcommand.
	// for example, if the key is "foo bar", then to access it our ankh.CLI
	// instalnnce must be access with "./cli foo bar". See the docs for CLI for
	// notes on how this changes some other behavior of the CLI as well.
	//
	// The factory should be as cheap as possible, ideally only allocating
	// a struct. The factory may be called multiple times in the course
	// of a command execution and certain events such as help require the instantiation
	// of all commands. Expensive initiliazation should be deferred to function
	// calls within the interface implementation
	Commands map[string]CommandFactory

	// HiddenCommands is a list of commands that are "hidden". Hidden
	// commands are not given to the help function callback and do not show up
	// in autocomplete. The values in the slice should be equivalent
	// to the Keys in the command map.
	HiddenCommands []string

	// Name defines the name of the CLI.
	Name string

	// Version of the CLI
	Version string

	// Autocomplete enables or disables subcommand auto-completion support.
	// This is enabled by default when NewCLI is called. Otherwise, this
	// must be enabled explicitely.
	//
	// Autocomplete requires the "Name" option to be set on CLI. This name
	// should be set exactly to the binary name that is autocompleted.
	//
	// AutocompleteInstall and AutocompleteUninstall are the global flag
	// names for installing and uninstalling the autocompletion handlers
	// for the user's shell. The flag should omit the hyphen(s) in front of
	// the value. Both single and double hyphens will automatically be supported
	// for the flag name. These default to `autocompelete-install`and
	// `autocomplete-uninstall` respectively.
	//
	// AutocompleteNoDefaultFlags is a boolean which contronls if the default auto-
	// complete flags like -help and -version are added to the output.
	//
	// AutoCompleteGlobalFlags are mapping of global flags for autocompletion.
	// The help and version flags are automatically added.
	Autocomplete               bool
	AutocompleteInstall        string
	AutocompleteUninstall      string
	AutocompleteNoDefaultFlags bool
	AutocompleteGlobalFlags    complete.Flags

	// HelpFunc is the function called to generate the generic help
	// text that is show if help must be shown for the CLI that doe'sn'
	// pertain to a specific command.
	HelpFunc HelpFunc

	// HelpWriter is used to print help text and version when requested.
	// Defaults to os.Stderr for backwards compatibility.
	// It is recommended that you set HelpWriter to os.Stdout, and
	// ErrorWriter to os.Stderr
	HelpWriter io.Writer

	// ErrorWriter is used to output errors when a command can not be run.
	// Defaults to the value of HelpWriter for backward compatibility.
	// It is recommended that you set HelpWriter to os.Stdout, and
	// ErrorWriter to os.Stderr.
	ErrorWriter io.Writer
	// contains filtered or unexported fields
}

Ankh contains the state necessary to run subcommands and parse the command line arguments.

Ankn also supports nested subcommands, such as "ankh foo bar". To use nested subcommands, the key in the Commands mapping below contains thee full subcommand. In this example, it would be "foo bar".

If you use a Ankh with nested with nested subcommands, some semantics change due to ambiguities: * We use longest prefix matching to find a matching subcommand. This means If you register "foo bar" and the user executes "ankh foo qux", the "foo" command will be executed with the arg "qux". It is up to you to handle these args. One option is to just return the special help return code `RunResultHelp` to display help and exit.

The help flag will list any subcommands that a command takes as well as the command's help itself. If there are no subcommands, it will note this. If the ankh.CLI instance itself has no subcommands, this entire section is omitted.

Any parent commands that don't exists are automatically created as No-op commnds that just show help for other subcommands. For example, if you only register "foo bar", then "foo" is automatically created.

func NewCLI

func NewCLI(app, version string) *CLI

NewCLI returns an new CLI instance with sensible defaults.

func (*CLI) IsHelp

func (c *CLI) IsHelp() bool

IsHelp returns whether or not the help flag is present withing the arguments.

func (*CLI) IsVersion

func (c *CLI) IsVersion() bool

isVersion returns whethere or not the version flag is present within the arguments.

func (*CLI) Run

func (c *CLI) Run() (int, error)

Run runs the actual CLI based on the arguments given

func (*CLI) Subcommand

func (c *CLI) Subcommand() string

Subcommand returns the subcommand that the CLI would execute. For example, a CLI from "--version version --help" would return a Subcommand of "version"

func (*CLI) SubcommandArgs

func (c *CLI) SubcommandArgs() []string

SubcommandArgs returns the arguments that will be passed to the subcommand.

type ColoredUi

type ColoredUi struct {
	OutputColor UiColor
	InfoColor   UiColor
	ErrorColor  UiColor
	WarnColor   UiColor
	Ui          Ui
}

ColoredUid is a Ui implementation that colors its output according to the given color schemes for the given types of output

func (*ColoredUi) Ask

func (u *ColoredUi) Ask(query string) (string, error)

func (*ColoredUi) AskSecret

func (u *ColoredUi) AskSecret(query string) (string, error)

func (*ColoredUi) Error

func (u *ColoredUi) Error(message string)

func (*ColoredUi) Info

func (u *ColoredUi) Info(message string)

func (*ColoredUi) Output

func (u *ColoredUi) Output(message string)

func (*ColoredUi) Warn

func (u *ColoredUi) Warn(message string)

type Command

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

	// 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 this can return documented
	// above that change behavior.
	Run(args []string) int

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

A command is a runnable sub-command of a CLI.

type CommandAutoComplete

type CommandAutoComplete interface {
	// AutocompleteArgs return the argument predictor for this command.
	// If argument completion is not supported, this should return
	// complete. PredictNothing.
	AutocompleteArgs() complete.Predictor

	// AutoCompleteFlags returns a mapping of supported flags and autocomplete
	// options for this command. The map key for the Flags map should be the
	// complete flag such as "-foo" or "--foo".
	AutoCompleteFlags() complete.Flags
}

CommandAutoComplete is an extension of Command that enables fine-grained autocompletion. Subcommand autocompletion will work even if tis interface is not implemented. By implementing this interface, more advanced autocompletion is enabled.

type CommandFactory

type CommandFactory func() (Command, error)

CommandFactory is a type of function that is a factory for commands. We need a factory because we way need to setup some state on the struct that implements the command itself

type CommandHelpTemplate

type CommandHelpTemplate interface {
	// HelpTemplate is the template in text/template format to use for
	// displaying the Help. The keys available are:
	//
	// * ", Help" - The help text itself
	// * ".Subcommands"
	//
	HelpTemplate() string
}

CommandHelpTemplate is an extension of Command that also has a function for returning a template for the help rather than the help itself. In this scenario, both help ahdn HelpTemplate should be implemented.

If CommandHelpTemplate isn't implemented, the Help is output as-is.

type ConcurrentUi

type ConcurrentUi struct {
	Ui Ui
	// contains filtered or unexported fields
}

ConcurrentUi is a wrapper around a Ui interface (and implements) that interface) making the underlying Ui conncurrency safe.

func (*ConcurrentUi) Ask

func (u *ConcurrentUi) Ask(query string) (string, error)

func (*ConcurrentUi) AskSecret

func (u *ConcurrentUi) AskSecret(query string) (string, error)

func (*ConcurrentUi) Error

func (u *ConcurrentUi) Error(message string)

func (*ConcurrentUi) Info

func (u *ConcurrentUi) Info(message string)

func (*ConcurrentUi) Output

func (u *ConcurrentUi) Output(message string)

func (*ConcurrentUi) Warn

func (u *ConcurrentUi) Warn(message string)

type HelpFunc

type HelpFunc func(map[string]CommandFactory) string

HelpFunc is the type of the function that is responsible for generating the help output when the CLI must show the general help text.

func BasicHelpFunc

func BasicHelpFunc(app string) HelpFunc

BasicHelpFunc generates some basic help output that is usually good enough for most CLI applications.

func FilteredHelpFunc

func FilteredHelpFunc(include []string, f HelpFunc) HelpFunc

FilteredHelpFunc will filter the commands to only include the keys in the include parameter.

type MockCommand

type MockCommand struct {
	// Settable
	HelpText     string
	RunResult    int
	SynopsisText string

	// Set by the command
	RunCalled bool
	RunArgs   []string
}

MockCommand is an implementation of Command that can be used for tests. It is publicly exported from this package in case you want to use it. externally.

func (*MockCommand) Help

func (c *MockCommand) Help() string

func (*MockCommand) Run

func (c *MockCommand) Run(args []string) int

func (*MockCommand) Synopsis

func (c *MockCommand) Synopsis() string

type MockCommandAutocomplete

type MockCommandAutocomplete struct {
	MockCommand

	// Settable
	AutocompleteArgsValue  complete.Predictor
	AutocompleteFlagsValue complete.Flags
}

MockCommandAutoComplete is an implementation of CommandAutoComplete

func (*MockCommandAutocomplete) AutoCompleteArgs

func (c *MockCommandAutocomplete) AutoCompleteArgs() complete.Predictor

func (*MockCommandAutocomplete) AutocompleteFlags

func (c *MockCommandAutocomplete) AutocompleteFlags() complete.Flags

type MockCommandHelpTemplate

type MockCommandHelpTemplate struct {
	MockCommand

	// Settable
	HelpTemplateText string
}

MockCommandHelpTemplate is an implementation of CommandHelpTemplate

func (*MockCommandHelpTemplate) HelpTemplate

func (c *MockCommandHelpTemplate) HelpTemplate() string

type MockUi

type MockUi struct {
	InputReader  io.Reader
	ErrorWriter  *syncBuffer
	OutputWriter *syncBuffer
	// contains filtered or unexported fields
}

MockUi is a mock UI taht is used for tests and is exported publicly for use in external tests if needed as well. Do not instantiate this directly since the buffers will be initialized on the first write. If there is no write then you will get a nil panic. Please use the NewMockUi() constructor function instead. You can fix your code with

sed -i -e 's/new(ankh.MockUi)/ankh.NewMockUi()/g' *_test.o

func NewMockUi

func NewMockUi() *MockUi

NewMockUi returns a fully initialized MockUi instance which is safer for concurrent use.

func (*MockUi) Ask

func (u *MockUi) Ask(query string) (string, error)

func (*MockUi) AskSecret

func (u *MockUi) AskSecret(query string) (string, error)

func (*MockUi) Error

func (u *MockUi) Error(message string)

func (*MockUi) Info

func (u *MockUi) Info(message string)

func (*MockUi) Output

func (u *MockUi) Output(message string)

func (*MockUi) Warn

func (u *MockUi) Warn(message string)

type PrefixedUi

type PrefixedUi struct {
	AskPrefix       string
	AskSecretPrefix string
	OutputPrefix    string
	InfoPrefix      string
	ErrorPrefix     string
	WarnPrefix      string
	Ui              Ui
}

func (*PrefixedUi) Ask

func (u *PrefixedUi) Ask(query string) (string, error)

func (*PrefixedUi) AskSecret

func (u *PrefixedUi) AskSecret(query string) (string, error)

func (*PrefixedUi) Error

func (u *PrefixedUi) Error(message string)

func (*PrefixedUi) Info

func (u *PrefixedUi) Info(message string)

func (*PrefixedUi) Output

func (u *PrefixedUi) Output(message string)

func (*PrefixedUi) Warn

func (u *PrefixedUi) Warn(message string)

type Ui

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

	// Ask secret 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(string)

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

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

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

Ui is an interface for interacting with the terminal or interface of a CLI. This abstraction does'nt have to be used, but helps provide a simple, layerable way to manage user interactions

type UiColor

type UiColor struct {
	Code int
	Bold bool
}

UiColor is a posix shell color code to use.

type UiWriter

type UiWriter struct {
	Ui Ui
}

UiWriter is an io.Writer implementation that can be used with loggers that writes every line of log output data to a Ui at the info level

func (*UiWriter) Write

func (w *UiWriter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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