-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnode.go
76 lines (73 loc) · 1.73 KB
/
node.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
package opts
import (
"flag"
"reflect"
)
// node is the main class, it contains
// all parsing state for a single set of
// arguments
type node struct {
err error
//embed item since an node can also be an item
item
parent *node
flagGroups []*itemGroup
flagNames map[string]bool //flag namespace covers all groups in this node
flagSkipShort map[string]bool
args []*item
envNames map[string]bool
userCfgPath bool
//external flagsets
flagsets []*flag.FlagSet
//subcommands
cmd *node
cmdname *string
cmdnameEnv string
cmds map[string]*node
//help
order []string
templates map[string]string
repo, author, version, summary string
repoInfer, authorInfer bool
lineWidth int
padAll bool
padWidth int
//pretend these are in the user struct :)
internalOpts struct {
Help bool
Version bool
Install bool
Uninstall bool
ConfigPath string
}
complete bool
}
func newNode(val reflect.Value) *node {
n := &node{
parent: nil,
//each cmd/cmd has its own set of names
flagNames: map[string]bool{},
flagSkipShort: map[string]bool{},
envNames: map[string]bool{},
cmds: map[string]*node{},
//these are only set at the root
order: defaultOrder(),
templates: map[string]string{},
//public defaults
lineWidth: 96,
padAll: true,
padWidth: 2,
}
//all new node's MUST be an addressable struct
t := val.Type()
if t.Kind() == reflect.Ptr {
t = t.Elem()
val = val.Elem()
}
if !val.CanAddr() || t.Kind() != reflect.Struct {
n.errorf("must be an addressable to a struct")
return n
}
n.item.val = val
return n
}