-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.go
66 lines (55 loc) · 1.29 KB
/
demo.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
package main
import (
"fmt"
"strings"
"github.com/ndaba1/gommander"
)
func main() {
app := gommander.App()
app.Author("vndaba").
Version("0.1.0").
Help("A demo usage of gommander")
app.SubCommand("greet").
Help("A simple cmd that greets provided name").
AddOption(
gommander.
NewOption("name").
Required(true).
Short('n').
Help("The name to greet").
AddArgument(
gommander.NewArgument("<name>"),
),
).
AddOption(
gommander.
NewOption("lang").
Short('l').
Help("The language to use").
Required(true).
AddArgument(
gommander.
NewArgument("<language>").
ValidateWith([]string{"ENGLISH", "SPANISH", "SWAHILI"}).
Default("ENGLISH"),
),
).Action(greetCb)
app.Set(gommander.IncludeHelpSubcommand, true)
app.Parse()
}
func greetCb(matches *gommander.ParserMatches) {
var completeGreeting strings.Builder
greeting, _ := matches.GetOptionValue("lang")
switch greeting {
case "ENGLISH":
completeGreeting.WriteString("Hello! ")
case "SPANISH":
completeGreeting.WriteString("Hola! ")
case "SWAHILI":
completeGreeting.WriteString("Jambo! ")
}
// Returns the value and error if any
name, _ := matches.GetOptionValue("name")
completeGreeting.WriteString(name)
fmt.Println(completeGreeting.String())
}