Golang flag.FlagSet.Init() function example
package flag
Init sets the name and error handling property for a flag set. By default, the zero FlagSet uses an empty name and the ContinueOnError error handling policy.
Golang flag.FlagSet.Init() function usage example
package main
import (
"flag"
"os"
)
const (
usage = `programname [--force] file [file ...]
Will molest the given file.
Unless force is specified, it will ask the user for permission before molesting...
`
)
func printUsage() {
os.Stderr.WriteString(usage)
os.Exit(0)
}
func main() {
force := false
flagSet := flag.NewFlagSet("", flag.ContinueOnError)
flagSet.Init("command-line", flag.ContinueOnError) // <-- here
flagSet.BoolVar(&force, "force", force, "--force is required to force everything down the pipe, without asking")
if err := flagSet.Parse(os.Args[1:]); err != nil {
printUsage()
}
if len(flagSet.Args()) == 0 {
printUsage()
}
}
Reference :
Advertisement
Something interesting
Tutorials
+15.6k Golang : ROT47 (Caesar cipher by 47 characters) example
+26.8k Golang : Find files by extension
+29.3k Golang : Save map/struct to JSON or XML file
+8.8k Golang : On lambda, anonymous, inline functions and function literals
+8.2k Golang : Add build version and other information in executables
+8.1k Golang : How To Use Panic and Recover
+5.7k Golang : Error handling methods
+20.8k Golang : Convert date string to variants of time.Time type examples
+9k Golang : Build and compile multiple source files
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+18.2k Golang : Get command line arguments