Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang custom flag usage
package main
import (
"flag"
"fmt"
)
func main() {
flag.String("a", "", "flag 1")
flag.String("b", "", "flag 2")
flag.String("c", "", "flag 3")
flag.Usage = func() {
flagSet := flag.CommandLine
fmt.Printf("Usage of %s:\n", "./tool")
order := []string{"a", "b", "c"}
for _, name := range order {
flag := flagSet.Lookup(name)
fmt.Printf("-%s\t%s\n", flag.Name,flag.Usage)
}
}
flag.Parse()
}
/*
[ output ]
Usage of ./tool:
-a flag 1
-b flag 2
-c flag 3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment