-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
example_test.go
77 lines (64 loc) · 1.9 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package getoptions_test
import (
"fmt"
"io"
"log"
"os"
"github.com/DavidGamba/go-getoptions"
)
var logger = log.New(io.Discard, "DEBUG: ", log.LstdFlags)
func Example() {
// Declare the variables you want your options to update
var debug bool
var greetCount int
var list map[string]string
// Declare the GetOptions object
opt := getoptions.New()
// Options definition
opt.Bool("help", false, opt.Alias("h", "?")) // Aliases can be defined
opt.BoolVar(&debug, "debug", false)
opt.IntVar(&greetCount, "greet", 0,
opt.Required(),
opt.Description("Number of times to greet."), // Set the automated help description
opt.ArgName("number"), // Change the help synopsis arg from the default <int> to <number>
)
opt.StringMapVar(&list, "list", 1, 99,
opt.Description("Greeting list by language."),
opt.ArgName("lang=msg"), // Change the help synopsis arg from <key=value> to <lang=msg>
)
// // Parse cmdline arguments os.Args[1:]
remaining, err := opt.Parse([]string{"-g", "2", "-l", "en='Hello World'", "es='Hola Mundo'"})
// Handle help before handling user errors
if opt.Called("help") {
fmt.Fprint(os.Stderr, opt.Help())
os.Exit(1)
}
// Handle user errors
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
fmt.Fprint(os.Stderr, opt.Help(getoptions.HelpSynopsis))
os.Exit(1)
}
// Use the passed command line options... Enjoy!
if debug {
logger.SetOutput(os.Stderr)
}
logger.Printf("Unhandled CLI args: %v\n", remaining)
// Use the int variable
for i := 0; i < greetCount; i++ {
fmt.Println("Hello World, from go-getoptions!")
}
// Use the map[string]string variable
if len(list) > 0 {
fmt.Printf("Greeting List:\n")
for k, v := range list {
fmt.Printf("\t%s=%s\n", k, v)
}
}
// Unordered output:
// Hello World, from go-getoptions!
// Hello World, from go-getoptions!
// Greeting List:
// en='Hello World'
// es='Hola Mundo'
}