modulepackage
0.0.0-20181222031601-6a79b845a666
Repository: https://github.com/codemodus/clip.git
Documentation: pkg.go.dev
# README
clip
go get -u github.com/codemodus/clip
Package clip provides simple command/subcommand structuring with a minimum of dependencies. The standard library flag.FlagSet type is leveraged often in this and related packages. If the FlagSet output is default, setting a help flag will result in usage output going to os.Stdout. Other parse errors will result in usage output going to os.Stderr as normal.
Usage
type Clip
func New(program string, flags *FlagSet, subcmds *CommandSet) *Clip
func (c *Clip) Parse(args []string) error
func (c *Clip) Run() error
func (c *Clip) Usage(depth int, err error) error
func (c *Clip) UsageLongHelp(err error) error
type Command
func NewCommand(flags *FlagSet, fn HandlerFunc, subcmds *CommandSet) *Command
func NewCommandNamespace(name string, subcmds *CommandSet) *Command
type CommandSet
func NewCommandSet(cmds ...*Command) *CommandSet
type FlagSet
func NewFlagSet(name string) *FlagSet
type HandlerFunc
type UsageError
func AsUsageError(err error) (UsageError, bool)
Setup
var (
globalCnf = newGlobalConf()
printCnf = newPrintConf("print")
otherCnf = newOtherConf("other")
)
cs := clip.NewCommandSet(
clip.NewCommand(printCnf.flagSet, runPrintFunc(printCnf, globalCnf), nil),
clip.NewCommand(otherCnf.flagSet, runOtherFunc(otherCnf), nil),
)
app := clip.New("myapp", globalCnf.flagSet, cs)
// emulate cli command 'myapp -v print -msg="hello, world"'
os.Args = []string{"myapp", "-v", "print", "-msg=hello, world"}
if err := app.Parse(os.Args); err != nil {
return app.UsageLongHelp(err)
}
return app.Run()
More Info
N/A
Documentation
View the GoDoc
Benchmarks
N/A
# Functions
AsUsageError is a convenience function for asserting that an error is a UsageError.
New constructs a pointer to an instance of Clip.
NewCommand constructs a pointer to an instance of Command.
NewCommandNamespace constructs a pointer to an instance of Command that is not intended to have a function associated with it.
NewCommandSet constructs a pointer to an instance of CommandSet.
NewFlagSet constructs a pointer to an instance of FlagSet.
# Structs
Clip manages a CLI program which may or may not have a related function and/or subcommands.
CommandSet manages a currently scheduled command and available commands.
# Interfaces
UsageError manages parse error related contextual information.
# Type aliases
Command is an alias of Clip.
FlagSet represents a set of defined flags.
HandlerFunc describes a function intended to be run as an endpoint of a called command.