-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.go
91 lines (79 loc) · 1.7 KB
/
run.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
package main
import (
"fmt"
"os"
"sort"
"strings"
)
type runInfo struct {
auth *authInfo
args []string
flags map[string]string
}
func run(auth *authInfo, args []string) error {
ri := &runInfo{
auth: auth,
args: args,
flags: make(map[string]string),
}
return ri.handle(rootCmd)
}
func (ri *runInfo) handle(cmd *cmdInfo) error {
var prefix []string
for {
// check flags
for _, flag := range cmd.flags {
flagN := "-" + flag.Name
found := false
// look in args for this
for p, arg := range ri.args {
if strings.ToLower(arg) == flagN {
// we have something!
if len(ri.args) < p+2 {
return fmt.Errorf("Flag %s needs to be followed by an argument: %s", flagN, flag.Usage)
}
ri.flags[flag.Name] = ri.args[p+1]
ri.args = append(ri.args[:p], ri.args[p+2:]...)
found = true
break
}
}
if found {
continue
}
if flag.Required {
return fmt.Errorf("Flag %s is required: %s", flagN, flag.Usage)
}
}
if cmd.cb != nil {
return cmd.cb(ri)
}
if len(ri.args) > 0 {
if v, ok := cmd.children[strings.ToLower(ri.args[0])]; ok {
prefix = append(prefix, ri.args[0])
ri.args = ri.args[1:]
cmd = v
continue
} else {
break
}
} else {
break
}
}
if len(cmd.children) == 0 {
return fmt.Errorf("invalid argument provided")
}
fmt.Fprintf(os.Stderr, "Error: invalid argument provided\r\n")
fmt.Fprintf(os.Stderr, "Please choose one of:\r\n")
list := make([]string, 0, len(cmd.children))
for k := range cmd.children {
list = append(list, k)
}
sort.Strings(list)
for _, k := range list {
fmt.Fprintf(os.Stderr, " * %s\r\n", strings.Join(append(prefix, k), " "))
}
os.Exit(2)
return nil
}