/
parse_context.go
77 lines (66 loc) · 1.81 KB
/
parse_context.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
package kingpinCli
import (
"github.com/alecthomas/kingpin"
"github.com/forj-oss/forjj-modules/cli/clier"
)
type ParseContext struct {
context *kingpin.ParseContext
}
type ParseContexter interface {
GetContext() *ParseContext
}
func (p *ParseContext) IsInvalidContext() bool {
if p == nil || p.context == nil {
return true
}
return false
}
// GetArgValue get value from cli, or if missing, ENV or if missing, defaults
func (p *ParseContext) GetArgValue(a clier.ArgClauser) (interface{}, bool) {
karg := a.(KArgClause).GetArg()
argClause := a.(*ArgClause)
for _, element := range p.context.Elements {
if a, ok := element.Clause.(*kingpin.ArgClause); ok && a == karg {
return *element.Value, true
}
}
if karg.HasEnvarValue() {
return karg.GetEnvarValue(), true
}
if argClause.hasDefaults() {
return argClause.getDefaults(), true
}
return nil, false
}
// GetFlagValue get value from cli, or if missing, ENV or if missing, defaults
func (p *ParseContext) GetFlagValue(f clier.FlagClauser) (interface{}, bool) {
kflag := f.(KFlagClause).GetFlag()
flagClause := f.(*FlagClause)
for _, element := range p.context.Elements {
if f, ok := element.Clause.(*kingpin.FlagClause); ok && f == kflag {
return *element.Value, true
}
}
if kflag.HasEnvarValue() {
return kflag.GetEnvarValue(), true
}
if flagClause.hasDefaults() {
return flagClause.getDefaults(), true
}
return nil, false
}
func (p *ParseContext) SelectedCommands() (res []clier.CmdClauser) {
cmds, _ := p.context.SelectedCmds()
res = make([]clier.CmdClauser, 0, len(cmds))
for _, cmd := range cmds {
res = append(res, &CmdClause{cmd})
}
return
}
func (p *ParseContext) GetParam(param_name string) (ret interface{}, err string) {
return
}
// Used by local unit test.
type ParseContextTester interface {
GetContext() *ParseContext
}