hedzr / cmdr Goto Github PK
View Code? Open in Web Editor NEWPOSIX-compliant command-line UI (CLI) parser and Hierarchical-configuration operations
Home Page: https://hedzr.com/cmdr-docs/
License: Apache License 2.0
POSIX-compliant command-line UI (CLI) parser and Hierarchical-configuration operations
Home Page: https://hedzr.com/cmdr-docs/
License: Apache License 2.0
Hi,
Maybe I'm missing what the feature ToggleGroup is but I thought that by setting a values like this would make the values mutually exclusive
{
BaseOpt: cmdr.BaseOpt{
Short: "L",
Full: "list",
Description: "list command",
Group: cStartup,
},
DefaultValue: false,
ToggleGroup: "target",
},
{
BaseOpt: cmdr.BaseOpt{
Short: "G",
Full: "grain",
Description: "grain command",
Group: cStartup,
},
DefaultValue: false,
ToggleGroup: "target",
},
but I can run both commands and I don't get an error.
Also, I would have wanted to have the option to get the remaining args that were not parsed just as in the official flag package.
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)
}
}
编译完如果 cmdrtest -o ""
就会 panic
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()
单引号的话就没问题~
谢谢你的作品,很棒的库,流式调用链相当舒服,使用中遇到了一个疑问:
const (
versionName = "0.0.1"
appName = "cmdrtest"
)
func buildRootCmd() (rootCmd *cmdr.RootCommand) {
root := cmdr.Root(appName, versionName)
// root.NewSubCommand().
// Titles("h", "help").
// Description("show help screen", "").
// Action(func(cmd *cmdr.Command, args []string) (err error) {
// fmt.Println("this is help text")
// os.Exit(0)
// return
// })
// root.NewFlag(cmdr.OptFlagTypeBool).
// Titles("h", "help").
// Description("show help screen", "").
// DefaultValue(false, "").
// OnSet(func(keyPath string, value interface{}) {
// fmt.Println("this is help text")
// os.Exit(0)
// return
// })
rootCmd = root.RootCommand()
return
}
func main() {
// To disable internal commands and flags, uncomment the following codes
cmdr.EnableVersionCommands = false
cmdr.EnableVerboseCommands = false
cmdr.EnableHelpCommands = false
cmdr.EnableGenerateCommands = false
cmdr.EnableCmdrCommands = false
rootCmd := buildRootCmd()
if err := cmdr.Exec(rootCmd); err != nil {
panic(err)
}
}
这个时候 -h
或者 --help
似乎是无效的 (Unknown flag: -h
)
但是直接运行会默认输出 usage,最后一行仍然会是:
Type '-h' or '--help' to get command help screen.
如果想要实现它,用 NewSubCommand
的话会不能带 -
符号,就和最后一行的输出相矛盾了。此时该用 NewFlag(cmdr.OptFlagTypeBool)
的 OnSet
吗?但总感觉它应该是 Command 范畴,因为可能需要实现 go help build
这种效果
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.