-
Notifications
You must be signed in to change notification settings - Fork 0
/
argument.go
42 lines (37 loc) · 1.07 KB
/
argument.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
package flag
import (
"fmt"
"strconv"
)
// Argument is used for config and returning parsed flags
type Argument struct {
// Name of argument
Name string `json:"name"`
// Rules for parsing argument values
Type ArgType `json:"type,omitempty"`
// Throws error if required and not provided
Required bool `json:"required,omitempty"`
// Populated value for argument
Value interface{} `json:"value,omitempty"`
}
// Parse attempts to set the given value for the given argument. Returns false if it does not meet type criteria
func (arg *Argument) Parse(value string) (err error) {
switch arg.Type {
case DEFAULT:
// String
arg.Value = value
return
case BOOL:
// Existence is sufficient
arg.Value = true
return
case INT:
if val, err := strconv.Atoi(value); err == nil {
arg.Value = val
} else {
return fmt.Errorf("Invalid value encountered. Cannot set <" + value + "> for INT type argument <" + arg.Name + ">")
}
return
}
return fmt.Errorf("Invalid value encountered. Cannot set <" + value + "> for " + string(arg.Type) + " argument <" + arg.Name + ">")
}