-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (33 loc) · 935 Bytes
/
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
package main
import (
"context"
"flag"
"fmt"
"strings"
"github.com/Thiht/go-command"
)
func main() {
root := command.Root().Flags(func(flagSet *flag.FlagSet) {
flagSet.Bool("verbose", false, "Enable verbose output")
}).Help("Example command")
root.SubCommand("echo").Action(EchoHandler).Flags(func(flagSet *flag.FlagSet) {
flagSet.String("case", "", "Case to use (upper, lower)")
})
root.Execute(context.Background())
}
func EchoHandler(ctx context.Context, flagSet *flag.FlagSet, args []string) int {
verbose := command.Lookup[bool](flagSet, "verbose")
textCase := command.Lookup[string](flagSet, "case")
if verbose {
fmt.Println("command echo called with case: " + textCase)
}
switch textCase {
case "upper":
fmt.Println(strings.ToUpper(strings.Join(args, " ")))
case "lower":
fmt.Println(strings.ToLower(strings.Join(args, " ")))
default:
fmt.Println(strings.Join(args, " "))
}
return 0
}