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
+12.5k Golang : Arithmetic operation with numerical slices or arrays example
+6.3k Golang : Selection sort example
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+6.9k Golang : Pat multiplexer routing example
+9.1k Golang : Handle sub domain with Gin
+12.1k Golang : Save webcamera frames to video file
+9.4k Golang : Generate EAN barcode
+12.1k Golang : convert(cast) string to integer value
+12.4k Elastic Search : Return all records (higher than default 10)
+7.5k Golang : Process json data with Jason package
+5.1k Swift : Convert (cast) Float to Int or Int32 value