scli

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 6 Imported by: 2

README

scli (Simple CLI)

Very simple implementation of a command line interface that supports sub commands. Is Based on ffcli but with a few features I do not need removed, and some added concepts from Cobra like command aliases and argument validation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnparsed         = errors.New("command tree is unparsed, can't run")
	ErrInvalidArguments = errors.New("invalid arguments")
)

Functions

This section is empty.

Types

type ArgsValidator

type ArgsValidator func(args []string) error

ArgsValidator is the function signature for representing an argument validator for Command's.

func CombineValidator

func CombineValidator(validators ...ArgsValidator) ArgsValidator

CombineValidator is used for combining multiple ArgsValidator's into one. It accepts multiple ArgsValidator functions and returns a single ArgsValidator, that checks all conditions in order they are passed.

func ExactArgs

func ExactArgs(n int) ArgsValidator

ExactArgs returns an error unless there are exactly N args.

func MaxArgs

func MaxArgs(n int) ArgsValidator

MaxArgs returns an error if there are more than N args.

func MinArgs

func MinArgs(n int) ArgsValidator

MinArgs returns an error if there are not at least N args.

func NoArgs

func NoArgs() ArgsValidator

NoArgs returns an error if any args are included.

func OnlyValidArgs

func OnlyValidArgs(validArgs []string) ArgsValidator

OnlyValidArgs returns an error if there are any args that are not contained in the validArgs slice.

func RangeArgs

func RangeArgs(min, max int) ArgsValidator

RangeArgs returns an error if the number of args is not in the expected range.

type Command

type Command struct {
	// Usage is a one liner usage message. First word of Usage is used for the Command's name.
	// Required for sub-commands.
	// Recommend syntax is:
	//
	//     cmd [flags] subcmd [flags] <required> [<optional> ...]
	Usage string

	// Aliases is a slice of alternate names that can be used for a sub command instead of the first word of usage.
	// Optional.
	Aliases []string

	// ShortHelp is a short description that is displayed in the global help -h output.
	// Optional, but recommended.
	ShortHelp string

	// LongHelp is a longer description that is displayed int the '<this-command> -h' output.
	// If not provided ShortHelp will be used in its place. Optional.
	LongHelp string

	// Subcommands is a slice of commands supported by Command.
	// Subcommands are optional and only needed if you application needs multiple commands.
	Subcommands []*Command

	// TODO
	UsageFunc func(c *Command) string

	// FlagSet for this command. Optional, but if none is provided,
	// an empty FlagSet will be defined to ensure -h works as expected.
	FlagSet *flag.FlagSet

	// ArgsValidator provides a validation function for arguments. There are multiple builtin validators as the
	// XArgs functions in this package.
	// Any error returned by ArgsValidator gets wrapped by an ErrInvalidArguments then is returned by Run or ParseAndRun.
	// When ArgsValidator returns an error the commands usage will be printed as well as the body of the error message.
	ArgsValidator ArgsValidator

	// Exec is the function that does the actual work, most Command's will implement this, unless they are just a
	// namespace for Subcommands.
	// The error returned by Exec will be bubble up and be returned by Run and ParseAndRun.
	// If flag.ErrHelp or ErrInvalidArguments is returned the commands usage will be printed to the output.
	Exec func(ctx context.Context, args []string) error
	// contains filtered or unexported fields
}

func (*Command) Name

func (c *Command) Name() string

Name of the command is derived from first word of Usage

func (*Command) Parse

func (c *Command) Parse(args []string) error

Parse the command line arguments for this command and all sub-commands

func (*Command) ParseAndRun

func (c *Command) ParseAndRun(ctx context.Context, args []string) error

ParseAndRun is a helper function to execute parse and run in a single invocation.

func (*Command) Run

func (c *Command) Run(ctx context.Context) (err error)

Run executes the previously selected command from a parsed Command.

type NoExecError

type NoExecError struct {
	Command *Command
}

func (NoExecError) Error

func (e NoExecError) Error() string

Jump to

Keyboard shortcuts

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