Golang flag.FlagSet.Args() function example
package flag
Args returns the non-flag arguments.
Golang flag.FlagSet.Args() 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:] // <-- here
if cmd == "help" && flags.NArg() == 2 {
cmd = flags.Arg(1)
cmdArgs = []string{"--help"}
}
}
fmt.Println(cmdArgs)
}
Reference :
Advertisement
Something interesting
Tutorials
+7.5k Golang : Create zip/ePub file without compression(use Store algorithm)
+17.6k Golang : delete and modify XML file content
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+15.9k Golang : Update database with GORM example
+6.7k Golang : Check if password length meet the requirement
+14k Golang : Reverse IP address for reverse DNS lookup example
+12k Golang : Convert a rune to unicode style string \u
+32.1k Golang : Validate email address with regular expression
+8.5k Golang : How to check variable or object type during runtime?
+26.8k Golang : Convert file content into array of bytes
+10k Golang : Channels and buffered channels examples
+15.3k Golang : Get all local users and print out their home directory, description and group id