-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflag.go
More file actions
84 lines (67 loc) · 2.1 KB
/
flag.go
File metadata and controls
84 lines (67 loc) · 2.1 KB
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
package clicmdflags
import (
"reflect"
"strings"
)
var flagPatterns = []string{
"--",
"-",
}
// {flagRequired} and {flagDefault} are MUTUALLY EXCLUSIVE
const flagName = "flagName" //
const flagRequired = "flagRequired" // required - needs inpuut from user
const flagDefault = "flagDefault" // default - would be the value if not set
const flagDescription = "flagDescription" //
const flagHidden = "flagHidden" //
type flag struct {
name string
typeName string
isRequired string
defaultValue string
defaultValueWasRequested bool
description string
isHidden string
wasSet bool
}
func isArgFlag(arg string) (bool, string) {
for _, flagPattern := range flagPatterns {
if strings.HasPrefix(arg, flagPattern) {
return true, strings.Replace(arg, flagPattern, "", 1)
}
}
return false, ""
}
func (thisRef *Command) getDefinedFlags() []flag {
result := []flag{}
runtimeStructRef, err := getRealRuntimeStruct(reflect.ValueOf(thisRef.Flags))
if err != nil {
return result
}
for i := 0; i < runtimeStructRef.NumField(); i++ {
newFlag := flag{
name: runtimeStructRef.Type().Field(i).Tag.Get(flagName),
typeName: runtimeStructRef.Type().Field(i).Type.Name(),
isRequired: runtimeStructRef.Type().Field(i).Tag.Get(flagRequired),
defaultValue: runtimeStructRef.Type().Field(i).Tag.Get(flagDefault),
description: runtimeStructRef.Type().Field(i).Tag.Get(flagDescription),
isHidden: runtimeStructRef.Type().Field(i).Tag.Get(flagHidden),
}
if _, tagFound := runtimeStructRef.Type().Field(i).Tag.Lookup(flagDefault); tagFound {
newFlag.defaultValueWasRequested = true
}
if len(strings.TrimSpace(newFlag.isRequired)) <= 0 {
newFlag.isRequired = "false"
}
result = append(result, newFlag)
}
return result
}
func (thisRef *Command) getRequriedFlags() []flag {
result := []flag{}
for _, definedFlag := range thisRef.getDefinedFlags() {
if definedFlag.isRequired == "true" {
result = append(result, definedFlag)
}
}
return result
}