Golang flag.FlagSet.Arg() function example
package flag
Arg returns the i'th (1st parameter) argument. Arg(0) is the first remaining argument after flags have been processed.
Golang flag.FlagSet.Arg() function usage example
package main
import (
"flag"
"fmt"
"os"
)
func main() {
flags := flag.NewFlagSet("", flag.ContinueOnError)
cmd := "help"
var cmdArgs []string
if err := flags.Parse(os.Args[1:]); err == nil && flags.NArg() != 0 {
cmd = flags.Arg(0)
cmdArgs = flags.Args()[1:] // <--- here
if cmd == "help" && flags.NArg() == 2 {
cmd = flags.Arg(1)
cmdArgs = []string{"--help"}
}
}
fmt.Println(cmdArgs)
}
Output :
$ go run flagsetarg.go help me
[--help]
Reference :
Advertisement
Something interesting
Tutorials
+15.8k Golang : Get digits from integer before and after given position example
+26.7k Golang : How to check if a connection to database is still alive ?
+16.3k Golang :Trim white spaces from a string
+7.7k Gogland : Where to put source code files in package directory for rookie
+17.5k Golang : Clone with pointer and modify value
+5.3k Golang : Get FX sentiment from website example
+17k Golang : Get number of CPU cores
+7.2k Golang : Use modern ciphers only in secure connection
+5.6k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+5.6k Golang : Shortening import identifier
+24.5k Golang : How to validate URL the right way
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem