This repository was archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand_helper.go
156 lines (129 loc) · 3.04 KB
/
command_helper.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package commander
import (
"errors"
"fmt"
"strings"
)
// CommandHelper is a helper struct
// CommandHandler.Execute will get this as an argument
// and you can access extra functions, farsed flags with this
type CommandHelper struct {
// If -d is defined
DebugMode bool
// If -v is defined
VerboseMode bool
// Boolean opts
Flags map[string]bool
// Other opts passed
Opts map[string]string
// Non-flag arguments
Args []string
argList []*Argument
}
// Log is a logger function for debug messages
// it prints a message if DebugeMode is true
func (c *CommandHelper) Log(message string) {
if c.DebugMode {
FmtPrintf("[Debug] %s\n", message)
}
}
// Arg return with an item from Flags based on the given index
// emtpy string if not exists
func (c *CommandHelper) Arg(index int) string {
if len(c.Args) > index {
return c.Args[index]
}
return ""
}
// Flag return with an item from Flags based on the given key
// false if not exists
func (c *CommandHelper) Flag(key string) bool {
if value, ok := c.Flags[key]; ok {
return value
}
return false
}
// Opt return with an item from Opts based on the given key
// empty string if not exists
func (c *CommandHelper) Opt(key string) string {
if value, ok := c.Opts[key]; ok {
return value
}
return ""
}
// ErrorForTypedOpt returns an error if the given value for
// the key is defined but not valid
func (c *CommandHelper) ErrorForTypedOpt(key string) error {
for _, arg := range c.argList {
if arg.Name != key {
continue
}
return arg.Error
}
return errors.New("key not found")
}
// TypedOpt return with an item from the predifined argument list
// based on the given key empty string if not exists
func (c *CommandHelper) TypedOpt(key string) interface{} {
for _, arg := range c.argList {
if arg.Name != key {
continue
}
return arg.Value
}
return ""
}
// Parse is a helper method that parses all passed arguments
// flags, opts and arguments
func (c *CommandHelper) Parse(flag []string) {
c.Flags = map[string]bool{}
c.Opts = map[string]string{}
if len(flag) < 2 {
return
}
arguments := flag[1:]
for _, arg := range arguments {
if len(arg) > 1 && arg[0:2] == "--" {
parts := strings.SplitN(arg[2:], "=", 2)
if len(parts) > 1 {
// has exact value
c.Opts[parts[0]] = parts[1]
} else {
c.Flags[parts[0]] = true
}
continue
}
if arg[0] == '-' {
for _, o := range []byte(arg[1:]) {
c.Flags[string(o)] = true
}
continue
}
c.Args = append(c.Args, arg)
}
if c.Flags["d"] {
c.DebugMode = true
}
if c.Flags["v"] {
c.VerboseMode = true
}
for _, arg := range c.argList {
if c.Opt(arg.Name) != "" {
arg.SetValue(c.Opt(arg.Name))
if arg.Error != nil {
errorMessage := fmt.Sprintf(
"Invalid argument: --%s=%s [%s]",
arg.Name, arg.OriginalValue, arg.Error,
)
if arg.FailOnError {
panic(errorMessage)
}
FmtPrintf("%s\n", errorMessage)
}
}
}
}
// AttachArgumentList binds an Argument list to CommandHelper
func (c *CommandHelper) AttachArgumentList(argumets []*Argument) {
c.argList = argumets
}