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
+7.7k Golang : Error reading timestamp with GORM or SQL driver
+19.9k Golang : Count JSON objects and convert to slice/array
+8.4k Golang : Generate Datamatrix barcode
+5.4k Golang : fmt.Println prints out empty data from struct
+11.1k Golang : How to determine a prime number?
+14.1k Golang : Check if a file exist or not
+16.6k Golang : Delete files by extension
+5k Google : Block or disable caching of your website content
+6.9k Default cipher that OpenSSL used to encrypt a PEM file
+15.3k nginx: [emerg] unknown directive "ssl"
+7.1k Golang : Get Alexa ranking data example
+11.6k Android Studio : Create custom icons for your application example