Golang:如何禁用自动标志排序?

coa*_*nor 7 go

使用flagpackage,我们可以指定一些命令行参数,如下所示:

import "flag"
fun main() {
    from := flag.String("from", "", "the path to be copied")
    to := flag.String("to", "", "where the data copied to")
    ldb := flag.String("db", "./ldb", "the database path used during copy")
    pwd := flag.String("pwd", "", "password to encrypt your data, default no encryption on your data"
    flag.Parse()
    ...
}
Run Code Online (Sandbox Code Playgroud)

当使用-h寻求帮助时,打印的消息似乎不是我提供的顺序:

-db string
    the database path used during copy (default "./ldb")
-from string
    the path to be copied
-pwd string
    password to encrypt your data, default no encryption on your data
-to string
    where the data copy to
Run Code Online (Sandbox Code Playgroud)

顺序不直观,是否有其他选项告诉 Golang:不要自动对我的参数进行排序!

Mar*_*her 6

输出按词法排序(https://golang.org/pkg/flag/#VisitAll):

VisitAll 按字典顺序访问命令行标志,为每个标志调用 fn。它访问所有标志,甚至是那些未设置的标志。

请参阅: https: //golang.org/src/flag/flag.go#L435

您可以使用 aflag.FlagSet并使用自定义Usage func().