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 :

http://golang.org/pkg/flag/#FlagSet.NArg

Advertisement