使用urfave/cli在Go中将`BoolFlags`组合成一个。

huangapple go评论98阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月26日 06:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75844993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定