my-cli-app help
PATH/TO/COMMAND
-
In comparison to
spf13/cobra
+spf13/pflag
- clean, lean, simple and small footprint code with similar functionality
- global flags are recognised regardless of the position: front, middle or at the end
- uses native Go structs defined by the user
- multiple comands in one line, each with its own flags
- memory is freed after you execute a command, it matters if 10+ commands in a daemon
-
In comparison to
Golang flag
- clean, lean, simple and small footprint code with similar functionality
- global flags are recognised regardless of the position: front, middle or at the end
- uses native Go structs defined by the user
- has command and sub-commands concept
go get github.com/codemodify/systemkit-clicmdflags
flagName |
Flag name |
flagRequired |
Marks a flag as required - needs inpuut from user |
flagDefault |
Would be the value if the flag is not set by the user |
flagDescription |
Text to show in help command |
// {flagRequired} and {flagDefault} are MUTUALLY EXCLUSIVE
type fourCmdFlags struct {
FourCmdFlags1 bool `flagName:"fourCmdFlags1" flagRequired:"true" flagDescription:"fourCmdFlags1 description"`
FourCmdFlags2 bool `flagName:"fourCmdFlags2" flagDefault:"false" flagDescription:"fourCmdFlags2 description"`
FourCmdFlags3 bool `flagName:"fourCmdFlags3" flagRequired:"true" flagDescription:"fourCmdFlags3 description"`
FourCmdFlags4 bool `flagName:"fourCmdFlags4" flagDefault:"false" flagDescription:"fourCmdFlags4 description"`
}
import (
"fmt"
"os"
"path/filepath"
"log"
clicmdflags "github.com/codemodify/systemkit-clicmdflags"
)
// AppRootCmdFlags -
type AppRootCmdFlags struct {
JSON bool `flagName:"json" flagDefault:"false" flagDescription:"Enables JSON output"`
Verbose bool `flagName:"verbose" flagDefault:"false" flagDescription:"Enables verbose output"`
}
// ExtendedInfoCmdFlags -
type ExtendedInfoCmdFlags struct {
DumpCPUInfo bool `flagName:"dumpCpuInfo" flagRequired:"true" flagDescription:"Outputs also CPU info"`
}
func main() {
var appRootCmd = &clicmdflags.Command{
Name: filepath.Base(os.Args[0]),
Description: "Displays PC information",
Examples: []string{
filepath.Base(os.Args[0]) + " -json",
filepath.Base(os.Args[0]) + " -json true",
},
Flags: AppRootCmdFlags{},
}
appRootCmd.AddCommand(&clicmdflags.Command{
Name: "version",
Description: "Displays product version",
Examples: []string{
filepath.Base(os.Args[0]) + " version",
},
Handler: func(command *clicmdflags.Command) {
fmt.Println("v1.0")
},
})
appRootCmd.AddCommand(&clicmdflags.Command{
Name: "info",
Description: "Displays extended information",
Examples: []string{
filepath.Base(os.Args[0]) + " info",
filepath.Base(os.Args[0]) + " info -dumpCpuInfo",
},
Flags: ExtendedInfoCmdFlags{},
Handler: func(command *clicmdflags.Command) {
fmt.Println("SSD size is 1TB")
flags, ok := command.Flags.(ExtendedInfoCmdFlags)
if ok && flags.DumpCPUInfo {
fmt.Println("CPU is 64bit capable")
}
},
})
if err := appRootCmd.Execute(); err != nil {
log.Fatal(err)
}
}