-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (77 loc) · 2.09 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"flag"
"fmt"
"github.com/phylake/go-cli"
"github.com/phylake/go-cli/cmd"
)
// Run `go run main.go`, `go run main.go punch`, `go run main.go punch -combo Shoryuken!`, etc.
func main() {
driver := cli.New(flag.ContinueOnError)
rootCmd := &cmd.Root{
Help: `Usage: ryu COMMAND [args]
A madeup CLI to demonstrate this framework.
Ryu is a character from Street Fighter II`,
SubCommandList: []cli.Command{
&PunchCmd{},
// A simple command so you don't have to implement all the methods
// of cli.Command
&cmd.Default{
NameStr: "version",
ShortHelpStr: "Print out a version string",
ExecuteFunc: func(args []string) bool {
fmt.Println("v0.0.1")
// since this command doesn't take any arguments it doesn't
// need a LongHelp() since we always return a successful
// execution
return true
},
},
},
}
if err := driver.RegisterRoot(rootCmd); err != nil {
panic(err)
}
if err := driver.ParseInput(); err != nil {
panic(err)
}
}
// PunchCmd implements cli.Command
type PunchCmd struct{}
func (cmd *PunchCmd) Name() string {
return "punch"
}
func (cmd *PunchCmd) ShortHelp() string {
return "Punch your shell"
}
func (cmd *PunchCmd) LongHelp() string {
return `NAME
punch - Punch your shell
SYNOPSIS
punch -combo <combo name>
DESCRIPTION
punch is a fake command illustrating how to build a nested command CLI
including parsing arguments and printing out this help text when a command
is invoked incorrectly`
}
// Return false if this command wasn't correctly invoked and LongHelp() will be
// printed out
func (cmd *PunchCmd) Execute(args []string) bool {
if len(args) > 0 {
var combo string
flagSet := flag.NewFlagSet("", flag.ContinueOnError)
flagSet.Usage = func() {
fmt.Println(cmd.LongHelp())
}
// don't use flag description since we're setting Usage
flagSet.StringVar(&combo, "combo", "", "")
// Parse will succeed or os.Exit
flagSet.Parse(args)
fmt.Println("You executed punch combo " + combo)
return true
}
return false
}
func (cmd *PunchCmd) SubCommands() []cli.Command {
return nil
}