如何使用Go的flag包打印位置参数的用法?

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

How to print usage for positional argument with Go's flag package?

问题

给定这个简单的Go程序,它需要一个命令行参数,我该如何改进它,以便flag.Usage()输出有用的信息?

package main

import (
    "flag"
    "fmt"
    "os"
)

func main() {
    flag.Parse()
    if len(flag.Args()) == 0 { 
        flag.Usage()
        os.Exit(1)
    }
    
    args := flag.Args()
    fmt.Println(args[0])
}

当前在没有给出参数的情况下的输出:

$ ./args
Usage of ./args:

(即使用说明为空,因为我找不到一种方法来告诉usage()函数哪些参数是必需的)。

我可以删除flag.Usage()并用以下内容替换:

fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "<argument>")

然而,如果flag.Usage()已经有了一个好的方法,我不想重复造轮子。特别是它已经处理了可选参数的情况:

$ ./args -foo
flag provided but not defined: -foo
Usage of ./args:

请注意,我只翻译了代码部分,其他内容不做翻译。

英文:

Given this simple Go program, which requires exactly one command line argument, how can I improve it so that flag.Usage() gives useful output?

package main

import (
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;os&quot;
)

func main() {
    flag.Parse()
    if len(flag.Args()) == 0 { 
        flag.Usage()
        os.Exit(1)
    }
    
    args := flag.Args()
    fmt.Println(args[0])
}

Current output with no arguments given:

<!-- language: lang-none -->

$ ./args
Usage of ./args:

(i.e. usage is empty, as I can find no way to tell the usage() function which parameters are required).

I can remove flag.Usage() and replace it with something like this:

fmt.Fprintln(os.Stderr, &quot;Usage:&quot;, os.Args[0], &quot;&lt;argument&gt;&quot;)

However, I don't want to reinvent the wheel if there's already a good way with flag.Usage(). Especially as it's already handling optional arguments:

<!-- language: lang-none -->

$ ./args -foo
flag provided but not defined: -foo
Usage of ./args:

答案1

得分: 26

flag.Usage()只会提供有关已定义标志的有用信息。因此,您可以通过var foo = flag.Int(...)将参数定义为标志。

另一种选择是定义自己的用法处理程序。下面是一个简单示例,它将打印自定义消息和所有已定义标志的默认值。这样,如果flag.Parse()失败,将打印您自定义的Usage

package main
import (
    "flag"
    "fmt"
    "os"
)

func myUsage() {
     fmt.Printf("Usage: %s [OPTIONS] argument ...\n", os.Args[0])
     flag.PrintDefaults()
}

func main() {
     flag.Usage = myUsage
     /* ... */
}

希望对您有所帮助!

英文:

flag.Usage() will only give you useful information about the defined flags. So you either define your arguments as flags via var foo = flag.Int(...).

Another option would be to define your own usage handler. see below for a simple example which will print a custom message and the defaults for all defined flags. This way you custom Usage will be printed in case flag.Parse() fails.

package main
import (
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;os&quot;
)

func myUsage() {
     fmt.Printf(&quot;Usage: %s [OPTIONS] argument ...\n&quot;, os.Args[0])
     flag.PrintDefaults()
}

func main() {
     flag.Usage = myUsage
     /* ... */
}

huangapple
  • 本文由 发表于 2015年8月7日 16:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/31873183.html
匿名

发表评论

匿名网友

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

确定