-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathapp.go
More file actions
43 lines (35 loc) · 796 Bytes
/
app.go
File metadata and controls
43 lines (35 loc) · 796 Bytes
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
package cli
import (
"os"
flags "github.com/jessevdk/go-flags"
)
// App defines the CLI application that will be run.
type App struct {
*flags.Parser
Name string
}
// New creates a new App, including default values.
func New(name string) *App {
app := &App{Name: name}
parser := flags.NewParser(nil, flags.Default)
parser.CommandHandler = func(command flags.Commander, args []string) error {
if v, ok := command.(initializer); ok {
v.init(app)
}
return command.Execute(args)
}
app.Parser = parser
return app
}
// RunMain parses arguments and runs commands
func (app *App) RunMain() {
if _, err := app.Parse(); err != nil {
if err, ok := err.(*flags.Error); ok {
if err.Type == flags.ErrHelp {
os.Exit(0)
}
app.WriteHelp(os.Stdout)
}
os.Exit(1)
}
}