英文:
Golang: How to disable automatically flag sorting?
问题
使用flag包,我们可以像这样指定一些命令行参数:
import "flag"
func main() {
    from := flag.String("from", "", "要复制的路径")
    to := flag.String("to", "", "数据复制到的位置")
    ldb := flag.String("db", "./ldb", "复制过程中使用的数据库路径")
    pwd := flag.String("pwd", "", "加密数据的密码,默认情况下不对数据进行加密")
    flag.Parse()
    ...
}
当使用-h参数获取帮助时,打印的消息似乎不是我提供的顺序:
-db string
    复制过程中使用的数据库路径 (默认为"./ldb")
-from string
    要复制的路径
-pwd string
    加密数据的密码,默认情况下不对数据进行加密
-to string
    数据复制到的位置
顺序不直观,是否有其他选项告诉Golang:“不要自动排序我的参数!”?
英文:
With flag package, we can specify some command line parameters like this:
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()
    ...
}
When use -h for help, the printed message seems not the order I provided:
-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
the order not intuitive, is there other options to tell Golang: Don't sort my parameter's automatically! ?
答案1
得分: 6
输出按字典顺序排序(https://golang.org/pkg/flag/#VisitAll):
> VisitAll按字典顺序访问命令行标志,对每个标志调用fn。它访问所有标志,即使那些未设置的标志也会被访问。
参见:https://golang.org/src/flag/flag.go#L435
您可以使用flag.FlagSet并使用自定义的Usage func()。
英文:
The output is lexically sorted (https://golang.org/pkg/flag/#VisitAll):
> VisitAll visits the command-line flags in lexicographical order, calling fn for each. It visits all flags, even those not set.
See: https://golang.org/src/flag/flag.go#L435
You could use a flag.FlagSet and use a custom Usage func().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论