Golang flag.FlagSet.Args() function example
package flag
Args returns the non-flag arguments.
Golang flag.FlagSet.Args() 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)
}
Reference :
Advertisement
Something interesting
Tutorials
+4.9k HTTP common errors and their meaning explained
+5.4k Golang : fmt.Println prints out empty data from struct
+13.8k Golang : unknown escape sequence error
+10.1k Golang : Identifying Golang HTTP client request
+11.6k Android Studio : Create custom icons for your application example
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+14.4k Golang : How to filter a map's elements for faster lookup
+19.2k Golang : Populate dropdown with html/template example
+21.4k Curl usage examples with Golang
+16.8k Golang : read gzipped http response
+7.4k Golang : How to detect if a sentence ends with a punctuation?