Documentation
¶
Overview ¶
Package cflag Wraps and extends go `flag.FlagSet` to build simple command line applications
- Support auto render a pretty help panel
- Allow to add shortcuts for flag option
- Allow binding named arguments
- Allow set required for argument or option
- Allow set validator for argument or option
Index ¶
- Variables
- func AddPrefix(name string) string
- func AddPrefixes(name string, shorts []string) string
- func IsZeroValue(opt *flag.Flag, value string) (bool, bool)
- func SetDebug(open bool)
- func WithDesc(desc string) func(c *CFlags)
- func WithVersion(version string) func(c *CFlags)
- type CFlags
- func (c *CFlags) AddArg(name, desc string, required bool, value interface{})
- func (c *CFlags) AddShortcuts(name string, shorts ...string)
- func (c *CFlags) AddValidator(name string, fn OptCheckFn)
- func (c *CFlags) Arg(name string) *FlagArg
- func (c *CFlags) BinFile() string
- func (c *CFlags) BindArg(arg *FlagArg)
- func (c *CFlags) ConfigOpt(name string, fn func(opt *FlagOpt))
- func (c *CFlags) MustParse(args []string)
- func (c *CFlags) Name() string
- func (c *CFlags) Parse(args []string) error
- func (c *CFlags) RemainArgs() []string
- func (c *CFlags) ShowHelp()
- func (c *CFlags) WithConfigFn(fns ...func(c *CFlags)) *CFlags
- type FlagArg
- type FlagOpt
- type OptCheckFn
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Debug = envutil.GetBool("CFLAG_DEBUG")
Debug mode
Functions ¶
func AddPrefixes ¶
AddPrefixes for render flag options help
func IsZeroValue ¶
IsZeroValue determines whether the string represents the zero value for a flag.
from flag.isZeroValue() and more return the second arg for check is string.
Types ¶
type CFlags ¶
type CFlags struct {
*flag.FlagSet
// Desc command description
Desc string
// Version command version number
Version string
// Example command usage examples
Example string
// LongHelp custom help
LongHelp string
// Func handler for the command
Func func(c *CFlags) error
// contains filtered or unexported fields
}
CFlags wrap and extends the go flag.FlagSet
eg:
// Can be set required and shorts on desc: // format1: desc;required cmd.IntVar(&age, "age", 0, "your age;true") // format2: desc;required;shorts cmd.IntVar(&age, "age", 0, "your age;true;a")
func New ¶
New create new instance.
Usage:
cmd := cflag.New(func(c *cflag.CFlags) {
c.Version = "0.1.2"
c.Desc = "this is my cli tool"
})
// binding opts and args
cmd.Parse(nil)
Example ¶
package main
import (
"github.com/gookit/goutil/cflag"
"github.com/gookit/goutil/cliutil"
)
func main() {
opts := struct {
age int
name string
str1 string
bol bool
}{}
c := cflag.New(func(c *cflag.CFlags) {
c.Desc = "this is a demo command"
c.Version = "0.5.1"
})
c.IntVar(&opts.age, "age", 0, "this is a int option;;a")
c.StringVar(&opts.name, "name", "", "this is a string option and required;true")
c.StringVar(&opts.str1, "str1", "def-val", "this is a string option with default value;;s")
c.AddArg("arg1", "this is arg1", true, nil)
c.AddArg("arg2", "this is arg2", true, nil)
c.AddArg("arg3", "this is arg3 with default", false, "def-val")
c.Func = func(c *cflag.CFlags) error {
// do something ...
cliutil.Infoln("hello, this is", c.Name())
cliutil.Infoln("option.age =", opts.age)
cliutil.Infoln("option.name =", opts.name)
cliutil.Infoln("option.str1 =", opts.str1)
cliutil.Infoln("arg1 =", c.Arg("arg1").String())
cliutil.Infoln("arg2 =", c.Arg("arg2").String())
cliutil.Infoln("arg3 =", c.Arg("arg3").String())
return nil
}
// c.MustParse(os.Args[1:])
c.MustParse(nil)
}
func (*CFlags) AddShortcuts ¶
AddShortcuts for option flag
func (*CFlags) AddValidator ¶
func (c *CFlags) AddValidator(name string, fn OptCheckFn)
AddValidator for a flag option
func (*CFlags) WithConfigFn ¶
WithConfigFn for command
type FlagArg ¶
type FlagArg struct {
// Value for the flag argument
*structs.Value
// Name of the argument
Name string
// Desc arg description
Desc string
// Index of the argument
Index int
// Required argument
Required bool
// Validator for check value
Validator func(val string) error
// contains filtered or unexported fields
}
FlagArg struct
type FlagOpt ¶
type FlagOpt struct {
// Shortcuts short names. eg: ["o", "a"]
Shortcuts []string
// Required option
Required bool
// Validator for check option value
Validator OptCheckFn
}
FlagOpt struct


