Golang flag.FlagSet.NArg() function example
package flag
NArg is the number of arguments remaining after flags have been processed.
Golang flag.FlagSet.NArg() 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:]
if cmd == "help" && flags.NArg() == 2 { // <-- here
cmd = flags.Arg(1)
cmdArgs = []string{"--help"}
}
}
fmt.Println(cmdArgs)
}
Reference :
Advertisement
Something interesting
Tutorials
+22.4k Golang : Read directory content with filepath.Walk()
+17k Golang : How to save log messages to file?
+9.9k Golang : Function wrapper that takes arguments and return result example
+5.3k Javascript : Shuffle or randomize array example
+37.7k Golang : Comparing date or timestamp
+30.8k Golang : Download file example
+9.4k Facebook : Getting the friends list with PHP return JSON format
+10.3k Golang : Convert file content to Hex
+28k Golang : Move file to another directory
+7.9k Setting $GOPATH environment variable for Unix/Linux and Windows
+9.5k Golang : Changing a RGBA image number of channels with OpenCV