forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
65 lines (57 loc) · 1.16 KB
/
app.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
package cli
import (
"os"
)
type App struct {
// The name of the program. Defaults to os.Args[0]
Name string
// Description of the program.
Usage string
// Version of the program
Version string
// List of commands to execute
Commands []Command
// List of flags to parse
Flags []Flag
// The action to execute when no subcommands are specified
Action func(context *Context)
}
func NewApp() *App {
return &App{
Name: os.Args[0],
Usage: "A new cli application",
Version: "0.0.0",
Action: helpCommand.Action,
}
}
func (a *App) Run(arguments []string) {
// append help to commands
a.Commands = append(a.Commands, helpCommand)
// append version to flags
a.Flags = append(
a.Flags,
BoolFlag{"version", "print the version"},
helpFlag{"show help"},
)
// parse flags
set := flagSet(a.Name, a.Flags)
err := set.Parse(arguments[1:])
if err != nil {
os.Exit(1)
}
context := NewContext(a, set, set)
checkHelp(context)
checkVersion(context)
args := context.Args()
if len(args) > 0 {
name := args[0]
for _, c := range a.Commands {
if c.HasName(name) {
c.Run(context)
return
}
}
}
// Run default Action
a.Action(context)
}