-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
81 lines (69 loc) · 1.79 KB
/
main.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
78
79
80
81
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"github.com/DavidGamba/go-getoptions"
)
var Logger = log.New(os.Stderr, "", log.LstdFlags)
func main() {
os.Exit(program(os.Args))
}
func program(args []string) int {
ctx, cancel, done := getoptions.InterruptContext()
defer func() { cancel(); <-done }()
opt := getoptions.New()
opt.Self("myscript", "Simple demo script")
opt.Bool("debug", false, opt.GetEnv("DEBUG"))
opt.Int("greet", 0, opt.Required(), opt.Description("Number of times to greet."))
opt.StringMap("list", 1, 99, opt.Description("Greeting list by language."))
opt.Bool("quiet", false, opt.GetEnv("QUIET"))
opt.HelpSynopsisArg("<name>", "Name to greet.")
opt.SetCommandFn(Run)
opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
if opt.Called("quiet") {
Logger.SetOutput(io.Discard)
}
err = opt.Dispatch(ctx, remaining)
if err != nil {
if errors.Is(err, getoptions.ErrorHelpCalled) {
return 1
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
if errors.Is(err, getoptions.ErrorParsing) {
fmt.Fprintf(os.Stderr, "\n"+opt.Help())
}
return 1
}
return 0
}
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
// Get arguments and options
name, _, err := opt.GetRequiredArg(args)
if err != nil {
return err
}
greetCount := opt.Value("greet").(int)
list := opt.Value("list").(map[string]string)
Logger.Printf("Running: %v", args)
// Use the int variable
for i := 0; i < greetCount; i++ {
fmt.Printf("Hello %s, from go-getoptions!\n", name)
}
// 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)
}
}
return nil
}