检查在Go中是否提供了标志位。

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

Check if Flag Was Provided in Go

问题

使用标志包,有没有一种好的方法来区分是否传递了字符串标志?

例如,当标志未传递时,我想将其设置为动态默认值。然而,如果标志被提供但值为"",我想将其设置为空。

目前我正在做以下操作:

flagHost = flag.String(flagHostFlagKey, "", "...")
...
setHostname := false
for _, arg := range os.Args {
	if arg == "-"+flagHostFlagKey {
		setHostname = true
	}
}

if !setHostname {
     ...

这种方法似乎可以正常工作,但有点丑陋。在保持标准标志包的情况下,有没有更好的方法?

英文:

With the flag package, is there a good way to distinguish if a string flag was passed?

For example, when the flag is not passed, I want to set it to a dynamic default value. However, I want to set it to empty if the flag was provided but with a value of "".

Current I am doing the following:

flagHost = flag.String(flagHostFlagKey, "", "...")
...
setHostname := false
for _, arg := range os.Args {
	if arg == "-"+flagHostFlagKey {
		setHostname = true
	}
}

if !setHostname {
     ...

Which seems to work fine, but is kind of ugly. Is there a better way while staying with the standard flag package?

答案1

得分: 52

使用flag.Visit()

描述:
Visit按字典顺序访问命令行标志,对每个标志调用fn。它只访问已设置的标志。

用法:

func isFlagPassed(name string) bool {
	found := false
	flag.Visit(func(f *flag.Flag) {
		if f.Name == name {
			found = true
		}
	})
	return found
}
英文:

Use the flag.Visit()

Description:
Visit visits the command-line flags in lexicographical order, calling fn for each. It visits only those flags that have been set.

use:

func isFlagPassed(name string) bool {
	found := false
	flag.Visit(func(f *flag.Flag) {
		if f.Name == name {
			found = true
		}
	})
	return found
}

答案2

得分: 30

内置的标志类型不支持区分默认值和对默认值的显式赋值。然而,flag包非常灵活,允许您使用flag.Value接口自定义类型来实现这一功能。

下面是一个完整的示例,其中包含一个字符串标志,用于记录是否已对其进行赋值。

package main

import (
	"flag"
	"fmt"
)

type stringFlag struct {
	set   bool
	value string
}

func (sf *stringFlag) Set(x string) error {
	sf.value = x
	sf.set = true
	return nil
}

func (sf *stringFlag) String() string {
	return sf.value
}

var filename stringFlag

func init() {
	flag.Var(&filename, "filename", "the filename")
}

func main() {
	flag.Parse()
	if !filename.set {
		fmt.Println("--filename not set")
	} else {
		fmt.Printf("--filename set to %q\n", filename.value)
	}
}

以下是一些示例运行结果:

$ go run a.go -filename=abc
--filename set to "abc"

$ go run a.go -filename=
--filename set to ""

$ go run a.go
--filename not set
英文:

The built-in flag types don't support distinguishing default values and explicit assignment to the default value. However, the flag package is quite flexible, and allows you to roll your own type that does, using the flag.Value interface.

Here's a full example that contains a string flag which records if it's been assigned to.

package main

import (
	"flag"
	"fmt"
)

type stringFlag struct {
	set   bool
	value string
}

func (sf *stringFlag) Set(x string) error {
	sf.value = x
	sf.set = true
	return nil
}

func (sf *stringFlag) String() string {
	return sf.value
}

var filename stringFlag

func init() {
	flag.Var(&filename, "filename", "the filename")
}

func main() {
	flag.Parse()
	if !filename.set {
		fmt.Println("--filename not set")
	} else {
		fmt.Printf("--filename set to %q\n", filename.value)
	}
}

Here's some example runs:

$ go run a.go -filename=abc
--filename set to "abc"

$ go run a.go -filename=
--filename set to ""

$ go run a.go
--filename not set

答案3

得分: 18

使用自定义标志类型(在本主题中的stringFlag示例)的问题是,它会稍微影响PrintDefaults输出(即--help)。例如,使用一个字符串用户名标志和一个stringFlag服务器名标志,--help的输出如下所示:

-server value
	server:port(默认值localhost:1234)
-username string
	username(默认值"kimmi")

请注意,从用户的角度来看,这两个都是字符串参数,但由于stringFlag不是字符串,它们以不同的方式呈现。

flag的Flagset具有一个内部映射,其中包括声明的标志(“formal”)和实际设置的标志(“actual”)。前者可以通过Lookup()获得,但遗憾的是后者没有暴露出来,否则你可以这样写:

var servername = flag.String("server", "localhost:8129", "server:port")

flag.Parse()

if f := flag.CommandLine.LookupActual("server"); f != nil {
	fmt.Printf("server设置为%#v\n", f)
} else {
	fmt.Printf("server未设置\n")
}

如果你想要一致的PrintDefaults()输出,似乎你能做的最好的办法是使用Visit来提取你自己对“actual”的视图(VisitAll对“formal”执行相同的操作):

var servername = flag.String("server", "localhost:8129", "server:port")

flag.Parse()

flagset := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { flagset[f.Name]=true } )

if flagset["server"] {
	fmt.Printf("通过标志设置了server\n")
} else {
	fmt.Printf("未明确设置server,使用默认值\n")
}
英文:

The issue with using a custom flag type (the stringFlag example in this thread) is you'll slightly upset the PrintDefaults output (i.e. --help). For example, with a string username flag and a stringFlag servername flag, --help looks like this:

-server value
	server:port (default localhost:1234)
-username string
	username (default "kimmi")

Note these are both string arguments as far as the user is concerned, but presented differently as a stringFlag is not a string.

flag's Flagset has an internal map that includes the flags that were declared ('formal') and those actually set ('actual'). The former is available via Lookup(), though alas the latter is not exposed, or you could just write:

var servername = flag.String("server", "localhost:8129", "server:port")

flag.Parse()

if f := flag.CommandLine.LookupActual("server"); f != nil {
	fmt.Printf("server set to %#v\n", f)
} else {
	fmt.Printf("server not set\n")
}

Seems like the best you can do, if you want consistent PrintDefaults() output, is to use Visit to extract your own view of 'actual' (VisitAll does the same thing with 'formal'):

var servername = flag.String("server", "localhost:8129", "server:port")

flag.Parse()

flagset := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { flagset[f.Name]=true } )

if flagset["server"] {
	fmt.Printf("server set via flags\n")
} else {
	fmt.Printf("server not explicitly set, using default\n")
}

答案4

得分: 3

要为标志(flag)使用动态默认值,可以将标志创建为默认值设置为动态值的形式:

func main() {
  flagHost = flag.String(flagHostFlagKey, computedHostFlag(), "...")
  flag.Parse()
  // 如果在命令行上未指定标志,则*flagHost等于computedHostFlag()的返回值。
  ...
}

使用这种方法,不需要检测标志是否在命令行上指定。同时,帮助信息会显示正确的默认值。

如果计算值依赖于其他标志或计算成本较高,则可以使用其他答案中提到的方法。

英文:

To use a dynamic default value for a flag, create the flag with the default set to the dynamic value:

func main() {
  flagHost = flag.String(flagHostFlagKey, computedHostFlag(), "...")
  flag.Parse()
  // *flagHost equals the return value from computedHostFlag() if 
  // the flag is not specified on the command line.
  ...
}

With this approach, it's not necessary to detect if the flag is specified on the command line. Also, help shows the correct default.

If the computed value depends on other flags or is expensive to calculate, then use the approach suggested in one of the other answers.

答案5

得分: 1

面临相同的问题,但是有一个更复杂的情况,涉及到布尔标志(bool flag)。在这种情况下,computedHostFlag()函数无法工作,因为你只能提供true或false来创建标志。使用"type stringFlag struct"的解决方案也不是最好的,因为它破坏了默认值的概念。

可以通过以下方式解决:创建两组具有不同默认值的标志,在解析后,只需检查第一组标志中的标志是否具有与第二组标志中的标志相同的值,如果相同,则表示该标志的值是由用户从命令行提供的。如果它们不同,则表示该标志是由默认设置的。

package main

import (
	"fmt"
	"flag"
)

func main() {
	args := []string{"-foo="}
	
	flagSet1 := flag.NewFlagSet("flagSet1", flag.ContinueOnError)
	foo1 := flagSet1.String("foo", "-", ``)
	boolFoo1 := flagSet1.Bool("boolfoo", false, ``)
	flagSet1.Parse(args)

	flagSet2 := flag.NewFlagSet("flagSet2", flag.ContinueOnError)
	foo2 := flagSet2.String("foo", "+", ``)
	boolFoo2 := flagSet2.Bool("boolfoo", true, ``)
	flagSet2.Parse(args)
	
	if *foo1 != *foo2 {
		fmt.Println("foo flag set by default")
	} else {
		fmt.Println("foo flag provided by user")
	}
		
	if *boolFoo1 != *boolFoo2 {
		fmt.Println("boolfoo flag set by default")
	} else {
		fmt.Println("boolfoo flag provided by user")
	}
}

playground链接:https://play.golang.org/p/BVceE_pN5PO ,对于真实的CLI执行,你可以像这样做:https://play.golang.org/p/WNvDaaPj585

英文:

Face with same problem, but have even complex case with bool flag, in this case computedHostFlag() not working, since you can provide to flag creation only true or false. "type stringFlag struct" solution also not the best, since ruin idea of default values.

Solve it in this way: create two sets of flags, with different default values, after parse - just check - if flag in first flagset have the same value that flag from second flagset - that it means that flag value was provided by user from command line. If they different - than this mean that flag was set by default.

package main

import (
	"fmt"
	"flag"
)

func main() {
	args := []string{"-foo="}
	
	flagSet1 := flag.NewFlagSet("flagSet1", flag.ContinueOnError)
	foo1 := flagSet1.String("foo", "-", ``)
	boolFoo1 := flagSet1.Bool("boolfoo", false, ``)
	flagSet1.Parse(args)

	flagSet2 := flag.NewFlagSet("flagSet2", flag.ContinueOnError)
	foo2 := flagSet2.String("foo", "+", ``)
	boolFoo2 := flagSet2.Bool("boolfoo", true, ``)
	flagSet2.Parse(args)
	
	if *foo1 != *foo2 {
		fmt.Println("foo flag set by default")
	} else {
		fmt.Println("foo flag provided by user")
	}
		
	if *boolFoo1 != *boolFoo2 {
		fmt.Println("boolfoo flag set by default")
	} else {
		fmt.Println("boolfoo flag provided by user")
	}
}

playground: https://play.golang.org/p/BVceE_pN5PO , for real CLI execution, you can do something like that: https://play.golang.org/p/WNvDaaPj585

答案6

得分: 1

https://stackoverflow.com/a/35809400/3567989 相同,但是使用指向字符串的指针而不是自定义结构体。如果未设置,则 *string 为 nil,如果设置了,则为非 nil。

package main

import (
    "flag"
    "fmt"
)

type stringPtrFlag struct {
    ptr **string
}

func (f stringPtrFlag) String() string {
    if *f.ptr == nil {
        return ""
    }
    return **f.ptr
}

func (f stringPtrFlag) Set(s string) error {
    *f.ptr = &s
    return nil
}

var filename *string

func init() {
    flag.Var(stringPtrFlag{&filename}, "filename", "the filename")
}

func main() {
    flag.Parse()
    if filename == nil {
        fmt.Println("--filename not set")
    } else {
        fmt.Printf("--filename set to %q\n", *filename)
    }
}
英文:

Same as https://stackoverflow.com/a/35809400/3567989 but with a pointer to a string instead of a custom struct. The *string is nil if unset, non-nil if set.

package main

import (
    "flag"
    "fmt"
)

type stringPtrFlag struct {
	ptr **string
}

func (f stringPtrFlag) String() string {
	if *f.ptr == nil {
		return ""
	}
	return **f.ptr
}

func (f stringPtrFlag) Set(s string) error {
	*f.ptr = &s
	return nil
}

var filename *string

func init() {
    flag.Var(stringPtrFlag{&filename}, "filename", "the filename")
}

func main() {
    flag.Parse()
    if filename == nil {
        fmt.Println("--filename not set")
    } else {
        fmt.Printf("--filename set to %q\n", *filename)
    }
}

答案7

得分: 0

我认为一种更可靠的方法是检查命令行参数(os.Args[1:])中的任何标志是否以"prefix" + str为前缀,所以函数如下:

func isInSlice(str string, list []string, prefix string) bool {
    for _, v := range list {
        if strings.HasPrefix(v, prefix + str) {
            return true
        }
    }
    return false
}
英文:

I think a more reliable way is to check whether any flag in the command-line parameters (os.Args[1:]) is prefixed by "prefix" + str, so the function:

func isInSlice(str string, list []string, prefix string) bool {
	for _, v := range list {
		if strings.HasPrefix(v, prefix + str) {
			return true
		}
	}
	return false
}

答案8

得分: -1

我发现我们有Lookup()方法:

func isFlagPassed(name string) bool {
  rs := flag.Lookup(name)
  return rs != nil 
}

完整的文档

英文:

I found that we have the Lookup() method:

func isFlagPassed(name string) bool {
  rs := flag.Lookup(name)
  return rs != nil 
}

Full docs

答案9

得分: -1

FlagSet在我的环境中(go版本go1.13.4 windows/amd64)没有LookupActual()函数,并且Ben L的答案中提到的内部映射actual无法直接访问。

我有一种方法可以使用reflect来检查标志是否已设置:

import "reflect"

fs := flag.NewFlagSet("the flags", flag.ExitOnError)
flag_name := "host"
host := fs.String(flag_name, "localhost", "specify the host address")
// other flags
fs.Parse(os.Args[1:])

if reflect.Indirect(reflect.ValueOf(fs)).FieldByName("actual").MapIndex(reflect.ValueOf(flag_name)).IsValid() {
     fmt.Printf("the host flag is set with value %v", *host)
} else {
     fmt.Printf("the host flag is not set")
}
英文:

The FlagSet does not have a function LookupActual() in my environment (go version go1.13.4 windows/amd64), and the internal map actual mentioned in Ben L's answer can not be accessed directly.

I have an approach to check if a flag is set using reflect:

import "reflect"

fs := flag.NewFlagSet("the flags", flag.ExitOnError)
flag_name := "host"
host := fs.String(flag_name, "localhost", "specify the host address")
// other flags
fs.Parse(os.Args[1:])

if reflect.Indirect(reflect.ValueOf(fs)).FieldByName("actual").MapIndex(reflect.ValueOf(flag_name)).IsValid() {
     fmt.Printf("the host flag is set with value %v", *host)
} else {
     fmt.Printf("the host flag is not set")
}

huangapple
  • 本文由 发表于 2016年3月5日 10:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35809252.html
匿名

发表评论

匿名网友

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

确定