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
+16.1k Golang : How to check if input from os.Args is integer?
+39.2k Golang : How to read CSV file
+8.4k Golang : Ackermann function example
+8.8k Golang : Take screen shot of browser with JQuery example
+5.4k Python : Delay with time.sleep() function example
+21.6k Golang : GORM create record or insert new record into database example
+6.9k Golang : Calculate BMI and risk category
+11.5k Golang : Handle API query by curl with Gorilla Queries example
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+4.8k Which content-type(MIME type) to use for JSON data
+8k Golang : Sort words with first uppercase letter
+11.3k Golang : Byte format example