我想通过更改stdout输出来使程序的帮助消息更加清晰。现在,我使用 flag.Usage 提供其他信息,但我也想更改输出标志的顺序。现在,标志按字母顺序排序,但是我需要将顺序更改为逻辑顺序。例如。 现在:
./mytool --help
-aaa
input file of aaa
-bbb
input file of bbb
-mode
job's mode
我希望:)
./mytool --help
-mode
job's mode
-aaa
input file of aaa
-bbb
input file of bbb
非常感谢您!
答案 0 :(得分:2)
根据source code of flag,不可能修改flag.PrintDefault()
的顺序。
但是,您可以编写自定义flag.Usage
函数,如以下示例所示:
package main
import (
"flag"
"fmt"
)
func main() {
flag.String("aaa", "foo", "input file of aaa")
flag.String("bbb", "foo", "input file of aaa")
flag.String("mode", "foo", "job's mode")
flag.Usage = func() {
flagSet := flag.CommandLine
fmt.Printf("Custom Usage of %s:\n", "./mytool")
order := []string{"mode", "aaa", "bbb"}
for _, name := range order {
flag := flagSet.Lookup(name)
fmt.Printf("-%s\n", flag.Name)
fmt.Printf(" %s\n", flag.Usage)
}
}
flag.Parse()
}
输出:
Custom Usage of ./mytool:
-mode
job's mode
-aaa
input file of aaa
-bbb
input file of aaa