Created
February 13, 2021 14:36
-
-
Save hahwul/b3870d58fd038afba05099ee9be86417 to your computer and use it in GitHub Desktop.
Golang custom flag usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
What is the difference between this and flag.PrintDefaults()
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works like a charm, thanks!