getopt-like behavior in Go

前端 未结 9 703
南笙
南笙 2021-02-07 11:33

How do I nicely parse a list of program parameters and automate handling \"--help\" and/or \"--version\" (such as \"program [-d value] [--abc] [FILE1]\") in Go?

9条回答
  •  伪装坚强ぢ
    2021-02-07 11:50

    go-flags is very complete, BSD licensed, and has a clear example.

    var opts struct {
          DSomething string `short:"d" description:"Whatever this is" required:"true"`
          ABC bool `long:"abc" description:"Something"`
    }
    
    fileArgs, err := flags.Parse(&opts)
    
    if err != nil {
        os.Exit(1)
    }
    

提交回复
热议问题