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.3k Golang : Not able to grep log.Println() output
+7.2k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+8.9k Golang : Sort lines of text example
+30.5k Get client IP Address in Go
+5.8k Golang : List all packages and search for certain package
+18.5k Golang : Write file with io.WriteString
+10.1k Golang : Compare files modify date example
+11.6k Golang : Concurrency and goroutine example
+5.3k Python : Convert(cast) string to bytes example
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+26.9k Golang : Force your program to run with root permissions