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

时间:2016-03-31 06:46:31

标签: go

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

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()
    ...
}

使用-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

订单不直观,还有其他选项告诉Golang:不要自动排序我的参数!

1 个答案:

答案 0 :(得分:3)

输出按字节顺序排序(https://golang.org/pkg/flag/#VisitAll):

  

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

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

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