Documentation
¶
Overview ¶
Package capp provides a simple command line application build.
- Support add multiple commands
- Support add aliases for command
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
*cflag.CFlags // save global flags
Name string
Desc string
// Version for app
Version string
// NameWidth max width for command name
NameWidth int
HelpWriter io.Writer
// OnAppFlagParsed hook func
OnAppFlagParsed func(app *App) bool
// AfterHelpBuild hook
AfterHelpBuild func(buf *strutil.Buffer)
// BeforeRun each command hook func
// - cmdArgs: input raw args for current command.
// - return false to stop run.
BeforeRun func(c *Cmd, cmdArgs []string) bool
// AfterRun command hook func
AfterRun func(c *Cmd, err error)
// contains filtered or unexported fields
}
App struct
func New ¶
New App instance
Usage:
app := capp.New(func(app *cflag.App) {})
app.Name = "mycli"
app.Version = "0.0.1"
app.Desc = "mycli is a command line tool"
func NewApp ¶
NewApp instance. alias of New()
Example ¶
package main
import (
"github.com/gookit/goutil/cflag/capp"
"github.com/gookit/goutil/dump"
)
func main() {
app := capp.NewApp()
app.Desc = "this is my cli application"
app.Version = "1.0.2"
var c1Opts = struct {
age int
name string
}{}
c1 := capp.NewCmd("demo", "this is a demo command")
c1.OnAdd = func(c *capp.Cmd) {
c.IntVar(&c1Opts.age, "age", 0, "this is a int option;;a")
c.StringVar(&c1Opts.name, "name", "", "this is a string option and required;true")
c.AddArg("arg1", "this is arg1", true, nil)
c.AddArg("arg2", "this is arg2", false, nil)
}
c1.Func = func(c *capp.Cmd) error {
dump.P(c1Opts, c.Args())
return nil
}
var c2Opts = struct {
str1 string
lOpt string
bol bool
}{}
c2 := capp.NewCmd("other", "this is another demo command")
{
c2.StringVar(&c2Opts.str1, "str1", "def-val", "this is a string option with default value;;s")
c2.StringVar(&c2Opts.lOpt, "long-opt", "", "this is a string option with shorts;;lo")
c2.Func = func(c *capp.Cmd) error {
dump.P(c2Opts)
return nil
}
}
app.Add(c1, c2)
app.Run()
}
func (*App) Add ¶
Add command(s) to app. panic if error.
NOTE: command object should create use NewCmd()
Usage:
app.Add(
cflag.NewCmd("cmd1", "desc1"),
cflag.NewCmd("cmd2", "desc2"),
)
Or:
app.Add(cflag.NewCmd("cmd1", "desc1"))
app.Add(cflag.NewCmd("cmd2", "desc2"))
func (*App) RunWithArgs ¶
RunWithArgs run app by input args
func (*App) WithConfigFn ¶
WithConfigFn config app
type Cmd ¶
type Cmd struct {
*cflag.CFlags
Name string
Desc string // desc for command, will sync set to CFlags.Desc
// Aliases name for command
Aliases []string
// OnAdd hook func. fire on add to App
// - you can add some cli options or arguments.
OnAdd func(c *Cmd)
// Func for run command, will call after options parsed. will sync set to CFlags.Func
Func func(c *Cmd) error
// contains filtered or unexported fields
}
Cmd for App
func (*Cmd) QuickRun ¶
func (c *Cmd) QuickRun()
QuickRun parse OS flags and run command, will auto handle error
func (*Cmd) WithConfigFn ¶
func (c *Cmd) WithConfigFn(fns ...CmdOptionFn) *Cmd
WithConfigFn config cmd, alias of ConfigCmd()
type CmdOptionFn ¶
type CmdOptionFn func(c *Cmd)
CmdOptionFn for one command
func WithAliases ¶
func WithAliases(aliases ...string) CmdOptionFn
WithAliases set aliases for command
Click to show internal directories.
Click to hide internal directories.