By flag
package we can easily achieve the program's command-line parameter markers,
then we look at how to use the Help command-line program,
usually parameter flag -h
or --help
form to use.
Automatically generated using the help
We only need to declare other parameter markers, and execution resolution, flag
the package will help us to automatically generate use help.
// main.go // output number 2, a large number of output Package main Import ( " In Flag " " FMT " " Math " ) FUNC main () { firstFlag: = flag.Float64 ( " First " , 0 , " The first number " ) secondFlag: = flag.Float64 ( " sECOND " , 0 , " second number " ) flag.Parse () max: = math.max (firstFlag *, * secondFlag) FMT.Print(max) }
Compiled after the implementation of a look:
E: \ GOPATH \ the src \ max> max.exe - Help the Usage of max.exe: -first a float first number -Second a float second number E: \ GOPATH \ the src \ max > = max.exe -first . 5 = -Second 66 66
Custom Use Help
Maybe you do not like or are not satisfied with the automatically generated using the help, then you can also try using a custom help. We use the same flag
package, but here rewritten flag.Usage
to implement custom use help.
// main.go Package main Import ( " In Flag " " FMT " ) const Help = ` Program Name: max Description: The number of output 2, output of a large number. Example: . 1 . Test - Help 2 max = -first. . 5 -Second = 66 ` FUNC main () { flag.Usage = FUNC () { fmt.Print (help) } flag.Parse () // here only used to help implement. }
Compiled after the implementation of a look:
E: \ GOPATH \ the src \ max> max.exe - Help Program Name: max Description: The number of output 2, output of a large number. Example: . 1 . Test - Help 2 . -First = max . 5 -Second = 66
reference