I want to make the help message by my program more clear by changing stdout output. Now I using of flag.Usage for provide of additional information but also I want to change of an order of output the flags. Now flags are sorted by alphabetical but I need to change an order to logical. E.g. Now:
./mytool --help
-aaa
input file of aaa
-bbb
input file of bbb
-mode
job's mode
I wish :)
./mytool --help
-mode
job's mode
-aaa
input file of aaa
-bbb
input file of bbb
Thank you so much!
According to the source code of flag, it is impossible to modify the order of flag.PrintDefault()
.
However, you can write a custom flag.Usage
function, like the following example:
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:
", "./mytool")
order := []string{"mode", "aaa", "bbb"}
for _, name := range order {
flag := flagSet.Lookup(name)
fmt.Printf("-%s
", flag.Name)
fmt.Printf(" %s
", flag.Usage)
}
}
flag.Parse()
}
The output:
Custom Usage of ./mytool:
-mode
job's mode
-aaa
input file of aaa
-bbb
input file of aaa