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
+12.8k Swift : Convert (cast) Int or int32 value to CGFloat
+6.4k Golang : Break string into a slice of characters example
+12.1k Golang : md5 hash of a string
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+10.5k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+30.8k Golang : Download file example
+6.9k Golang : Calculate BMI and risk category
+9.1k Golang : Gonum standard normal random numbers example
+20.2k Golang : Convert seconds to human readable time format example
+15.4k Golang : Find location by IP address and display with Google Map
+14.4k Golang : How to convert a number to words
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example