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
+9.4k Facebook : Getting the friends list with PHP return JSON format
+11.6k Golang : Surveillance with web camera and OpenCV
+11.6k Golang : Simple file scaning and remove virus example
+14.5k Golang : Rename directory
+11k Golang : Create S3 bucket with official aws-sdk-go package
+5.6k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+7.5k Golang : Process json data with Jason package
+23.5k Golang : Check if element exist in map
+8.7k Golang : Combine slices but preserve order example
+32.7k Golang : Regular Expression for alphanumeric and underscore