cli is a simple, fast package for building command line apps in Go. It's a wrapper around the "flag" package.
Declare a struct type along with a fields you want to capture as flags.
typeEchostruct {
Echoedstring`flag:"echoed, echo this string"`
}Package understands all basic types supported by flag's package xxxVar functions: int, int64, uint, uint64, float64, bool, string, time.Duration. Types implementing flag.Value interface are also supported.
typeCustomDatestringfunc (c*CustomDate) String() string {
returnfmt.Sprint(*c)
}
func (c*CustomDate) Set(valuestring) error {
dateRegex:=`^20\d{2}(\/|-)(0[1-9]|1[0-2])(\/|-)(0[1-9]|[12][0-9]|3[01])$`ifok, err:=regexp.MatchString(dateRegex, value); err!=nil||!ok {
returnerrors.New("from parameter is not a valid date")
}
*c=CustomDate(value)
returnnil
}
typeEchoWithDatestruct {
Echoedstring`flag:"echoed, echo this string"`EchoWithDateCustomDate`flag:"echoDate, echo this date too"`
}Now we need to make our type implement the cli.Command interfacem, which requires three methods:
func (c*Echo) Synopsis() string {
return"Echo the input string."
}
func (c*Echo) Run() {
fmt.Println(c.Echoed)
}Maybe write sample command runs:
func (c*Echo) Help() []string {
return []string{"echoprogram -echoed=\"echo this\"",
"echoprogram -echoed=\"or echo this\""}
}We can set default command to run
c.SetDefault("echo")After all of this, we can run them like this:
funcmain() {
c:=cli.New("echoer", "1.0.0")
c.Authors= []string{"authors goes here"}
c.Add(
&Echo{
Echoed: "default string",
})
c.Run(os.Args)
}