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
+5.7k Golang : Frobnicate or tweaking a string example
+7.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+5.2k JavaScript/JQuery : Redirect page examples
+6.9k Golang : How to setup a disk space used monitoring service with Telegram bot
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+7.7k Gogland : Where to put source code files in package directory for rookie
+14.4k Golang : How to convert a number to words
+7.7k Golang : Mapping Iban to Dunging alphabets
+3.7k Golang : Switch Redis database redis.NewClient
+11.9k Golang : How to parse plain email text and process email header?
+5.3k Javascript : Shuffle or randomize array example
+8.3k Golang : Configure Apache and NGINX to access your Go service example