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
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+10k Golang : Setting variable value with ldflags
+18.4k Golang : Read binary file into memory
+21.8k SSL : How to check if current certificate is sha1 or sha2
+13.9k Golang : convert(cast) string to float value
+7.1k Nginx : How to block user agent ?
+9k Golang : Inject/embed Javascript before sending out to browser example
+9.2k Golang : Create and shuffle deck of cards example
+12.6k Golang : flag provided but not defined error
+26.9k Golang : Force your program to run with root permissions
+19.2k Golang : Populate dropdown with html/template example
+30.9k error: trying to remove "yum", which is protected