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
+16k Golang : Get sub string example
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+26.9k Golang : Force your program to run with root permissions
+5.2k Golang : Convert lines of string into list for delete and insert operation
+12.8k Golang : Convert int(year) to time.Time type
+8.8k Golang : Executing and evaluating nested loop in html template
+5.4k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+8.8k Golang : Random integer with rand.Seed() within a given range
+9.5k Golang : Accessing content anonymously with Tor
+15.2k Golang : Save(pipe) HTTP response into a file
+8.7k Golang : How to join strings?
+7.1k Golang : Get Alexa ranking data example