英文:
combining `BoolFlags` into one using urfave/cli in go
问题
我有一个可以获取-f/--foo
或-b/--bar
参数的代码。参数解析是通过urfave/cli包完成的,这是第二受欢迎的Go参数解析器。我可以像这样运行程序:go run . -f -b
,但不能像go run . -fb
这样运行。有没有办法使用urfave/cli使其支持go run . -fb
的方式?如果不可能,有哪个Go模块可以实现这一点?
代码:
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
var foo_count, bar_count bool
app := &cli.App{
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "foo",
Usage: "Foo",
Aliases: []string{"f"},
Destination: &foo_count,
},
&cli.BoolFlag{
Name: "bar",
Usage: "Bar",
Aliases: []string{"b"},
Destination: &bar_count,
},
},
Action: func(cCtx *cli.Context) error {
fmt.Println("foo_count", foo_count)
fmt.Println("bar_count", bar_count)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
测试:
$ go run . -f
foo_count true
bar_count false
$ go run . -b
foo_count false
bar_count true
$ go run . -bf
Incorrect Usage: flag provided but not defined: -bf
NAME:
main - A new cli application
USAGE:
main [global options] command [command options] [arguments...]
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--foo, -f Foo (default: false)
--bar, -b Bar (default: false)
--help, -h show help
2023/03/25 15:54:00 flag provided but not defined: -bf
exit status 1
英文:
I have a code that can get arguments of -f/--foo
or -b/--bar
. Argument parsing is done via urfave/cli package, which is the second most popular Go argument parser. I can run my program like go run . -f -b
but not like go run . -fb
Is there a way that I make it work with go run . -fb
using urfave/cli?
If it is not possible, what go module can make this possible?
code:
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
var foo_count, bar_count bool
app := &cli.App{
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "foo",
Usage: "Foo",
Aliases: []string{"f"},
Destination: &foo_count,
},
&cli.BoolFlag{
Name: "bar",
Usage: "Bar",
Aliases: []string{"b"},
Destination: &bar_count,
},
},
Action: func(cCtx *cli.Context) error {
fmt.Println("foo_count", foo_count)
fmt.Println("bar_count", bar_count)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
tests
$ go run . -f
foo_count true
bar_count false
$ go run . -b
foo_count false
bar_count true
$ go run . -bf
Incorrect Usage: flag provided but not defined: -bf
NAME:
main - A new cli application
USAGE:
main [global options] command [command options] [arguments...]
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--foo, -f Foo (default: false)
--bar, -b Bar (default: false)
--help, -h show help
2023/03/25 15:54:00 flag provided but not defined: -bf
exit status 1
答案1
得分: 1
这个问题在包的示例中有具体的解答。在创建 cli 时,添加 UseShortOptionHandling: true
。
文档中提到:
可以通过在应用程序配置中使用 UseShortOptionHandling
布尔值,或者通过将其附加到命令配置来实现。
英文:
This question is addressed specifically in one of the package examples. Add UseShortOptionHandling: true
when you create your cli.
https://github.com/urfave/cli/blob/main/docs/v2/examples/combining-short-options.md
From the docs:
This can be done using the UseShortOptionHandling
bool in your app configuration, or for individual commands by attaching it to the command configuration.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论