/
define.go
140 lines (116 loc) · 2.98 KB
/
define.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
package gcli
import (
"fmt"
"strconv"
)
// constants for error level 0 - 4
const (
VerbQuiet uint = iota // don't report anything
VerbError // reporting on error
VerbWarn
VerbInfo
VerbDebug
VerbCrazy
)
// constants for hooks event, there are default allowed event names
const (
EvtAppInit = "app.init"
EvtAppBefore = "app.run.before"
EvtAppAfter = "app.run.after"
EvtAppError = "app.run.error"
EvtCmdInit = "cmd.init"
EvtCmdBefore = "cmd.run.before"
EvtCmdAfter = "cmd.run.after"
EvtCmdError = "cmd.run.error"
EvtAppPrepareAfter = "app.prepare.after"
// EvtStop = "stop"
)
// GlobalOpts global flags
type GlobalOpts struct {
verbose uint // message report level
NoColor bool
showVer bool
showHelp bool
// dont display progress
noProgress bool
// close interactive confirm
noInteractive bool
// StrictMode use strict mode for parse flags
// If True(default):
// - short opt must be begin "-", long opt must be begin "--"
// - will convert like "-ab" to "-a -b"
// - will check invalid arguments, like to many arguments
strictMode bool
// command auto completion mode.
// eg "./cli --cmd-completion [COMMAND --OPT ARG]"
inCompletion bool
}
// Runner interface
type Runner interface {
// Config(c *Command)
Run(c *Command, args []string) error
}
// CmdFunc definition
type CmdFunc func(c *Command, args []string) error
// Run implement the Runner interface
func (f CmdFunc) Run(c *Command, args []string) error {
return f(c, args)
}
// Commander interface definition
type Commander interface {
// Creator for create new command
Creator() *Command
// Prepare bind Flags or Arguments for the command
Prepare(c *Command)
// Execute(c *Command, args []string) error
Run(c *Command, args []string) error
}
// Executor definition
type Executor interface {
}
// Executor definition
type RunningAble struct {
}
/*************************************************************************
* options: some special flag vars
* - implemented flag.Value interface
*************************************************************************/
// Ints The int flag list, implemented flag.Value interface
type Ints []int
// String to string
func (s *Ints) String() string {
return fmt.Sprintf("%v", *s)
}
// Set new value
func (s *Ints) Set(value string) error {
intVal, err := strconv.Atoi(value)
if err == nil {
*s = append(*s, intVal)
}
return err
}
// Strings The string flag list, implemented flag.Value interface
type Strings []string
// String to string
func (s *Strings) String() string {
return fmt.Sprintf("%v", *s)
}
// Set new value
func (s *Strings) Set(value string) error {
*s = append(*s, value)
return nil
}
// Booleans The bool flag list, implemented flag.Value interface
type Booleans []bool
// String to string
func (s *Booleans) String() string {
return fmt.Sprintf("%v", *s)
}
// Set new value
func (s *Booleans) Set(value string) error {
boolVal, err := strconv.ParseBool(value)
if err == nil {
*s = append(*s, boolVal)
}
return err
}