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
+7.7k Golang : Mapping Iban to Dunging alphabets
+15.3k Golang : Get all local users and print out their home directory, description and group id
+19.3k Golang : Get RGBA values of each image pixel
+7.7k Gogland : Where to put source code files in package directory for rookie
+14.2k Golang : Fix image: unknown format error
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+5.4k Javascript : How to loop over and parse JSON data?
+20.2k Golang : Compare floating-point numbers
+12.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+19.8k Golang : Append content to a file
+12.1k Golang : Save webcamera frames to video file
+27.2k Golang : Find files by name - cross platform example