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
+5.6k Golang : Detect words using using consecutive letters in a given string
+55.3k Golang : Unmarshal JSON from http response
+7k Golang : Find the shortest line of text example
+11.1k Golang : Read until certain character to break for loop
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+16.3k Golang :Trim white spaces from a string
+10.3k Golang : Wait and sync.WaitGroup example
+8.6k Golang : Convert(cast) []byte to io.Reader type
+39.2k Golang : How to read CSV file
+31.6k Golang : Get local IP and MAC address
+16.6k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error