我使用这个库https://github.com/jessevdk/go-flags全局参数的命令使用中间人标志
用于我的应用程序的命令可能是这样:
ex list events
所以,我有我的包装命令
type ExCommand struct {
List list.ListCommand `command:"list" description:"list events" subcommands-optional:"true"`
}
列表命令
type ListCommand struct {
ExCommand ExCommand `command:"events" description:"list events"`
Config string `short:"c" long:"config" description:"config file" optional:"yes"`
}
Ex命令
type EventsCommand struct {
}
func (c *ListCommand) Execute(args []string) error {
fmt.Println("execute list")
for _, val := range args {
fmt.Println(val)
}
fmt.Printf("c: %s\n", c.Config)
return nil
}
func (c *ExCommand) Execute(args []string) error {
fmt.Println("list events")
for _, val := range args {
fmt.Println(val)
}
return nil
}
所以我喜欢做的,就是有像
verbose
config
terse
是可以在任何命令来运行全局选项的几个选项。我似乎无法弄清楚是否有办法通过这个库来做到这一点。有没有人有这方面的经验?我可以将Config
添加到每个低级别命令中,在这种情况下列出EventsCommand,但似乎我通过在每个低级别命令中添加而不是更高级别的ListCommand或ExCommand来重复自己。提前致谢!
这是有道理的。但是,如何访问ListCommand中的详细或Terse标志? ListCommand的Execute函数运行'func(c * ListCommand)Execute(args [] string)error'并且ListCommand无法访问详细或Terse权限? – Crystal
这是正确的。我的示例中的详细和简洁可以通过上面示例中的defaultOptions来访问。您可以使该全局能够在您的程序中访问这些选项。 –
我要用另一种可能性来编辑我的答案,以达到您想要实现的目标。稍等一会儿。 –