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
+5.9k Golang : Denco multiplexer example
+14.4k Golang : How to filter a map's elements for faster lookup
+6.1k Golang : Dealing with backquote
+9.5k Golang : Extract or copy items from map based on value
+8.5k Golang : How to check if input string is a word?
+9.7k Golang : Load ASN1 encoded DSA public key PEM file example
+8.4k Your page has meta tags in the body instead of the head
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+6.3k Golang : Selection sort example
+4.8k Which content-type(MIME type) to use for JSON data
+11.6k Golang : Convert(cast) float to int
+16.9k Golang : Read integer from file into array