-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
39 lines (32 loc) · 1001 Bytes
/
cli.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
package cli
import (
"os"
"github.com/alecthomas/kong"
)
var cli struct {
Create createCmd `kong:"cmd,help='Create a new project from a project template'"`
Help helpCmd `kong:"cmd,help='Show this help text'"`
Init initCmd `kong:"cmd,help='Convert an existing directory into a project template'"`
Lint lintCmd `kong:"cmd,help='Check a project template for mistakes'"`
Update updateCmd `kong:"cmd,help='Apply template changes to an existing project'"`
Version versionCmd `kong:"cmd,help='Print Tribble’s version number'"`
}
// ParseAndRun parses command line arguments, then runs the matching command.
func ParseAndRun(version string) {
ctx := parse(os.Args[1:])
cli.Help.ctx = ctx
cli.Version.version = version
err := ctx.Run()
ctx.FatalIfErrorf(err)
}
func parse(args []string) *kong.Context {
parser, err := kong.New(&cli,
kong.UsageOnError(),
)
if err != nil {
panic(err)
}
ctx, err := parser.Parse(args)
parser.FatalIfErrorf(err)
return ctx
}