Go的`flag`包可以打印用法吗?

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

Can Go's `flag` package print usage?

问题

我可以帮你翻译这段内容:

我能否自定义Go的flag包,以便打印自定义的使用说明字符串?我有一个应用程序,当前的输出如下:

  1. Usage of ./mysqlcsvdump:
  2. -compress-file=false: 是否压缩连接
  3. -hostname="": 数据库主机
  4. -outdir="": 输出存储位置
  5. -password="": 数据库密码
  6. -port=3306: 数据库端口
  7. -single-transaction=true: 是否将所有操作包装在一个事务中
  8. -skip-header=false: 是否包含列标题
  9. -user="root": 数据库用户

我希望得到以下形式的输出:

  1. Usage: ./mysqlcsvdump [options] [table1 table2 ... tableN]
  2. 参数:
  3. -compress-file=false: 是否压缩连接
  4. -hostname="": 数据库主机
  5. -outdir="": 输出存储位置
  6. -password="": 数据库密码
  7. -port=3306: 数据库端口
  8. -single-transaction=true: 是否将所有操作包装在一个事务中
  9. -skip-header=false: 是否包含列标题
  10. -user="root": 数据库用户
英文:

Is it possible for me to customize Go's flag package so that it prints a custom usage string? I have an application with current output

  1. Usage of ./mysqlcsvdump:
  2. -compress-file=false: whether compress connection or not
  3. -hostname="": database host
  4. -outdir="": where output will be stored
  5. -password="": database password
  6. -port=3306: database port
  7. -single-transaction=true: whether to wrap everything in a transaction or not.
  8. -skip-header=false: whether column header should be included or not
  9. -user="root": database user

and would rather have something like

  1. Usage: ./mysqlcsvdump [options] [table1 table2 ... tableN]
  2. Parameters:
  3. -compress-file=false: whether compress connection or not
  4. -hostname="": database host
  5. -outdir="": where output will be stored
  6. -password="": database password
  7. -port=3306: database port
  8. -single-transaction=true: whether to wrap everything in a transaction or not.
  9. -skip-header=false: whether column header should be included or not
  10. -user="root": database user

答案1

得分: 119

是的,你可以通过修改flag.Usage来实现:

  1. var Usage = func() {
  2. fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
  3. flag.PrintDefaults()
  4. }
  5. Usage函数会向标准错误输出打印一个使用说明其中包含所有定义的命令行标志该函数是一个变量可以更改为指向自定义函数的指针
  6. `flag`之外的示例用法
  7. flag.Usage = func() {
  8. fmt.Fprintf(os.Stderr, "This is not helpful.\n")
  9. }
英文:

Yes, you can do that by modifying flag.Usage:

> var Usage = func() {
> fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
>
> flag.PrintDefaults()
> }
>
> Usage prints to standard error a usage message documenting all defined
> command-line flags. The function is a variable that may be changed to
> point to a custom function.

Example use from outside of flag:

  1. flag.Usage = func() {
  2. fmt.Fprintf(os.Stderr, "This is not helpful.\n")
  3. }

答案2

得分: 6

如果您想完全自定义您的使用方式,您需要通过使用flag.VisitAll()来重新实现flag.Usage()函数来遍历所有解析的标志。例如:

  1. flag.Usage = func() {
  2. fmt.Fprintf(os.Stderr, "自定义帮助 %s:\n", os.Args[0])
  3. flag.VisitAll(func(f *flag.Flag) {
  4. fmt.Fprintf(os.Stderr, " %v\n", f.Usage) // f.Name, f.Value
  5. })
  6. }

这段代码将自定义帮助信息输出到标准错误流,并遍历所有解析的标志,打印它们的使用方式。

英文:

If you want full customisation over your usage, you need to reimplement the flag.Usage() function by using flag.VisitAll() to iterate through all the parsed flags. For example:

  1. flag.Usage = func() {
  2. fmt.Fprintf(os.Stderr, "Custom help %s:\n", os.Args[0])
  3. flag.VisitAll(func(f *flag.Flag) {
  4. fmt.Fprintf(os.Stderr, " %v\n", f.Usage) // f.Name, f.Value
  5. })
  6. }

答案3

得分: 6

在2018年2月,Go 1.10 更新了flag输出的默认目标:

> 默认的 Usage 函数现在将其第一行输出到 CommandLine.Output(),而不是默认使用 os.Stderr,这样可以正确地重定向使用 CommandLine.SetOutput 的客户端的使用消息。

(参见 flag.go

因此,如果你想自定义你的 flag 使用方式,请不要假设使用 os.Stderr,而是使用类似以下的方式:

  1. flag.Usage = func() {
  2. w := flag.CommandLine.Output() // 可能是 os.Stderr,但不一定是
  3. fmt.Fprintf(w, "使用方式:%s ...自定义前导... \n", os.Args[0])
  4. flag.PrintDefaults()
  5. fmt.Fprintf(w, "...自定义尾声... \n")
  6. }
英文:

In Feb 2018, Go 1.10 updated the default destination of flag output:

> The default Usage function now prints its first line of output to
> CommandLine.Output() instead of assuming os.Stderr, so that the usage
> message is properly redirected for clients using
> CommandLine.SetOutput.

(see also flag.go)

So if you want to customize your flag usage, don't assume os.Stderr, instead use something like:

  1. flag.Usage = func() {
  2. w := flag.CommandLine.Output() // may be os.Stderr - but not necessarily
  3. fmt.Fprintf(w, "Usage of %s: ...custom preamble... \n", os.Args[0])
  4. flag.PrintDefaults()
  5. fmt.Fprintf(w, "...custom postamble ... \n")
  6. }

答案4

得分: 1

最后,在Go 1.16.4中,我们有:

// Output返回用于输出用法和错误消息的目标。如果未设置output或设置为nil,则返回os.Stderr。
func (f *FlagSet) Output() io.Writer {
if f.output == nil {
return os.Stderr
}
return f.output
}

因此,默认情况下它使用os.Stderr,否则使用设置的输出。

英文:

And finally, in Go 1.16.4 we have:

  1. // Output returns the destination for usage and error messages. os.Stderr is returned if
  2. // output was not set or was set to nil.
  3. func (f *FlagSet) Output() io.Writer {
  4. if f.output == nil {
  5. return os.Stderr
  6. }
  7. return f.output
  8. }

So, by default it uses os.Stderr, otherwise what was set to the output.

huangapple
  • 本文由 发表于 2014年5月19日 03:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/23725924.html
匿名

发表评论

匿名网友

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

确定