const (
versionName = "0.0.1"
appName = "cmdrtest"
)
func buildRootCmd() (rootCmd *cmdr.RootCommand) {
root := cmdr.Root(appName, versionName)
root.NewFlag(cmdr.OptFlagTypeString).
Titles("o", "output-file").
Description("output file", "").
DefaultValue("", "").
OnSet(func(keyPath string, value interface{}) {
fmt.Println(keyPath)
os.Exit(0)
return
})
rootCmd = root.RootCommand()
return
}
func main() {
rootCmd := buildRootCmd()
if err := cmdr.Exec(rootCmd,
cmdr.WithBuiltinCommands(false, false, false, false, false),
); err != nil {
panic(err)
}
}
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/hedzr/cmdr.(*ptpkg).preprocessPkg(0xc0002c6000, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x5)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/ptpkg.go:170 +0x33a
github.com/hedzr/cmdr.(*ptpkg).processTypeString(0xc0002c6000, 0xc00002a0c0, 0x3, 0x4, 0xffffffffffffffff, 0xc0000220d1)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/ptpkg.go:280 +0x6e
github.com/hedzr/cmdr.(*ptpkg).tryExtractingValue(0xc0002c6000, 0xc00002a0c0, 0x3, 0x4, 0xc0002784e8, 0x9e509f)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/ptpkg.go:100 +0x112
github.com/hedzr/cmdr.(*ExecWorker).flagsMatched(0xc0002b8120, 0xc0002c6000, 0xc0002981c0, 0xc00002a0c0, 0x3, 0x4, 0x9c8ede, 0xc0002c6000, 0xc0002c6028)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec_match.go:133 +0x5f
github.com/hedzr/cmdr.(*ExecWorker).flagsMatching(0xc0002b8120, 0xc0002c6000, 0xc0002981c0, 0xc00022be48, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x0, 0x0)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec_match.go:99 +0x3ce
github.com/hedzr/cmdr.(*ExecWorker).xxTestCmd(0xc0002b8120, 0xc0002c6000, 0xc00022be48, 0xc0002981c0, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x1, 0xc000134060)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec.go:244 +0x15c
github.com/hedzr/cmdr.(*ExecWorker).InternalExecFor(0xc0002b8120, 0xc0002981c0, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x0)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec.go:194 +0x2da
github.com/hedzr/cmdr.Exec(0xc0002981c0, 0xc00022bf38, 0x1, 0x1, 0x43e6c1, 0x1081ec0)
$GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec.go:88 +0x9e
main.main()