using go cli flags

#520
Raw
Author
Anonymous
Created
June 21, 2022, 6:04 p.m.
Expires
Never
Size
563 bytes
Hits
67
Syntax
Go
Private
No
package main

import (
	"flag"
	"fmt"
)

var name = flag.String("name", "World", "A name to say hello to.")

var spanish bool

func init() {
	flag.BoolVar(&spanish, "spanish", false, "Use Spanish language.")
	flag.BoolVar(&spanish, "s", false, "Use Spanish language.")
}

func main() {
	flag.Parse()

	flag.VisitAll(func(flag *flag.Flag) {
		format := "\t-%s: %s (Default: '%s') \n"
		fmt.Printf(format, flag.Name, flag.Usage, flag.DefValue)
	})

	if spanish {
		fmt.Printf("hola %s!\n", *name)
	} else {
		fmt.Printf("hello %s!\n", *name)
	}
}