Flag command line parsing in golang

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

Flag command line parsing in golang

问题

我不确定我理解这个例子的推理(取自这里),也不知道它试图传达关于Go语言的什么信息:

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. )
  6. func main() {
  7. f := flag.NewFlagSet("flag", flag.ExitOnError)
  8. f.Bool("bool", false, "this is bool flag")
  9. f.Int("int", 0, "this is int flag")
  10. visitor := func(a *flag.Flag) {
  11. fmt.Println(">", a.Name, "value=", a.Value)
  12. }
  13. fmt.Println("Visit()")
  14. f.Visit(visitor)
  15. fmt.Println("VisitAll()")
  16. f.VisitAll(visitor)
  17. // set flags
  18. f.Parse([]string{"-bool", "-int", "100"})
  19. fmt.Println("Visit() after Parse()")
  20. f.Visit(visitor)
  21. fmt.Println("VisitAll() after Parse()")
  22. f.VisitAll(visitor)
  23. }

在他们的设置基础上添加一个类似于下面的内容:

  1. int_val := f.Get("int")

以获取命名参数似乎更有用。我对Go完全不熟悉,所以只是试图熟悉这门语言。

英文:

I'm not sure I understand the reasoning behind this example (taken from here), nor what it is trying to communicate about the Go language:

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. )
  6. func main() {
  7. f := flag.NewFlagSet("flag", flag.ExitOnError)
  8. f.Bool("bool", false, "this is bool flag")
  9. f.Int("int", 0, "this is int flag")
  10. visitor := func(a *flag.Flag) {
  11. fmt.Println(">", a.Name, "value=", a.Value)
  12. }
  13. fmt.Println("Visit()")
  14. f.Visit(visitor)
  15. fmt.Println("VisitAll()")
  16. f.VisitAll(visitor)
  17. // set flags
  18. f.Parse([]string{"-bool", "-int", "100"})
  19. fmt.Println("Visit() after Parse()")
  20. f.Visit(visitor)
  21. fmt.Println("VisitAll() after Parse()")
  22. f.VisitAll(visitor)
  23. }

Something along the lines of the setup they have but then adding a

  1. int_val := f.get("int")

to get the named argument would seem more useful. I'm completely new to Go, so just trying to get acquainted with the language.

答案1

得分: 57

这是一个使用flag包的复杂示例。通常情况下,标志是这样设置的:

  1. package main
  2. import "flag"
  3. // 注意,变量是指针类型
  4. var strFlag = flag.String("long-string", "", "描述")
  5. var boolFlag = flag.Bool("bool", false, "标志的描述")
  6. func init() {
  7. // 使用短标志设置长标志的示例
  8. flag.StringVar(strFlag, "s", "", "描述")
  9. }
  10. func main() {
  11. flag.Parse()
  12. println(*strFlag, *boolFlag)
  13. }
英文:

This is complicated example of using flag package. Typically flags set up this way:

  1. package main
  2. import "flag"
  3. // note, that variables are pointers
  4. var strFlag = flag.String("long-string", "", "Description")
  5. var boolFlag = flag.Bool("bool", false, "Description of flag")
  6. func init() {
  7. // example with short version for long flag
  8. flag.StringVar(strFlag, "s", "", "Description")
  9. }
  10. func main() {
  11. flag.Parse()
  12. println(*strFlag, *boolFlag)
  13. }

huangapple
  • 本文由 发表于 2013年11月4日 13:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/19761963.html
匿名

发表评论

匿名网友

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

确定