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
+6.9k Golang : Normalize email to prevent multiple signups example
+8.3k Golang : Number guessing game with user input verification example
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+26.9k Golang : Force your program to run with root permissions
+48.1k Golang : How to convert JSON string to map and slice
+20.7k Golang : Read directory content with os.Open
+7.8k Golang : Lock executable to a specific machine with unique hash of the machine
+5.6k Golang : Detect words using using consecutive letters in a given string
+3.4k Golang : Fix go-cron set time not working issue
+11.5k CodeIgniter : Import Linkedin data
+36.3k Golang : How to split or chunking a file to smaller pieces?
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame