File: model.go

package info (click to toggle)
golang-gopkg-alecthomas-kingpin.v3 3.0~git20180227.b8d601d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 420 kB
  • sloc: makefile: 6
file content (298 lines) | stat: -rw-r--r-- 6,202 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// nolint: golint
package kingpin

import (
	"fmt"
	"strconv"
	"strings"
)

// Data model for Kingpin command-line structure.

type FlagGroupModel struct {
	Flags []*ClauseModel
}

func (f *FlagGroupModel) FlagByName(name string) *ClauseModel {
	for _, flag := range f.Flags {
		if flag.Name == name {
			return flag
		}
	}
	return nil
}

func (f *FlagGroupModel) FlagSummary() string {
	out := []string{}
	count := 0
	for _, flag := range f.Flags {
		if flag.Name != "help" {
			count++
		}
		if flag.Required {
			if flag.IsBoolFlag() {
				if flag.IsNegatable() {
					out = append(out, fmt.Sprintf("--[no-]%s", flag.Name))
				} else {
					out = append(out, fmt.Sprintf("--%s", flag.Name))
				}
			} else {
				out = append(out, fmt.Sprintf("--%s=%s", flag.Name, flag.FormatPlaceHolder()))
			}
		}
	}
	if count != len(out) {
		out = append(out, T("[<flags>]"))
	}
	return strings.Join(out, " ")
}

type ClauseModel struct {
	Name        string
	Help        string
	Short       rune
	Default     []string
	PlaceHolder string
	Required    bool
	Hidden      bool
	Value       Value
	Cumulative  bool
}

func (c *ClauseModel) String() string {
	return c.Value.String()
}

func (c *ClauseModel) IsBoolFlag() bool {
	return isBoolFlag(c.Value)
}

func (c *ClauseModel) IsNegatable() bool {
	bf, ok := c.Value.(BoolFlag)
	return ok && bf.BoolFlagIsNegatable()
}

func (c *ClauseModel) FormatPlaceHolder() string {
	if c.PlaceHolder != "" {
		return c.PlaceHolder
	}
	if len(c.Default) > 0 {
		ellipsis := ""
		if len(c.Default) > 1 {
			ellipsis = "..."
		}
		if _, ok := c.Value.(*stringValue); ok {
			return strconv.Quote(c.Default[0]) + ellipsis
		}
		return c.Default[0] + ellipsis
	}
	return strings.ToUpper(c.Name)
}

type ArgGroupModel struct {
	Args []*ClauseModel
}

func (a *ArgGroupModel) ArgSummary() string {
	depth := 0
	out := []string{}
	for _, arg := range a.Args {
		h := "<" + arg.Name + ">"
		if arg.Cumulative {
			h += " ..."
		}
		if !arg.Required {
			h = "[" + h
			depth++
		}
		out = append(out, h)
	}
	if len(out) == 0 {
		return ""
	}
	out[len(out)-1] = out[len(out)-1] + strings.Repeat("]", depth)
	return strings.Join(out, " ")
}

type CmdGroupModel struct {
	Commands []*CmdModel
}

func (c *CmdGroupModel) FlattenedCommands() (out []*CmdModel) {
	for _, cmd := range c.Commands {
		if cmd.OptionalSubcommands {
			out = append(out, cmd)
		}
		if len(cmd.Commands) == 0 {
			out = append(out, cmd)
		}
		out = append(out, cmd.FlattenedCommands()...)
	}
	return
}

type CmdModel struct {
	Name                string
	Aliases             []string
	Help                string
	Depth               int
	Hidden              bool
	Default             bool
	OptionalSubcommands bool
	Parent              *CmdModel
	*FlagGroupModel
	*ArgGroupModel
	*CmdGroupModel
}

func (c *CmdModel) String() string {
	return c.CmdSummary()
}

func (c *CmdModel) CmdSummary() string {
	out := []string{}
	for cursor := c; cursor != nil; cursor = cursor.Parent {
		text := cursor.Name
		if cursor.Default {
			text = "*" + text
		}
		if flags := cursor.FlagSummary(); flags != "" {
			text += " " + flags
		}
		if args := cursor.ArgSummary(); args != "" {
			text += " " + args
		}
		out = append([]string{text}, out...)
	}
	return strings.Join(out, " ")
}

// FullCommand is the command path to this node, excluding positional arguments and flags.
func (c *CmdModel) FullCommand() string {
	out := []string{}
	for i := c; i != nil; i = i.Parent {
		out = append([]string{i.Name}, out...)
	}
	return strings.Join(out, " ")
}

type ApplicationModel struct {
	Name    string
	Help    string
	Version string
	Author  string
	*ArgGroupModel
	*CmdGroupModel
	*FlagGroupModel
}

func (a *ApplicationModel) AppSummary() string {
	summary := a.Name
	if flags := a.FlagSummary(); flags != "" {
		summary += " " + flags
	}
	if args := a.ArgSummary(); args != "" {
		summary += " " + args
	}
	if len(a.Commands) > 0 {
		summary += " <command>"
	}
	return summary
}

func (a *ApplicationModel) FindModelForCommand(cmd *CmdClause) *CmdModel {
	if cmd == nil {
		return nil
	}
	path := []string{}
	for c := cmd; c != nil; c = c.parent {
		path = append([]string{c.name}, path...)
	}
	var selected *CmdModel
	cursor := a.CmdGroupModel
	for _, component := range path {
		for _, cmd := range cursor.Commands {
			if cmd.Name == component {
				selected = cmd
				cursor = cmd.CmdGroupModel
				break
			}
		}
	}
	if selected == nil {
		panic("this shouldn't happen")
	}
	return selected
}

func (a *Application) Model() *ApplicationModel {
	return &ApplicationModel{
		Name:           a.Name,
		Help:           a.Help,
		Version:        a.version,
		Author:         a.author,
		FlagGroupModel: a.flagGroup.Model(),
		ArgGroupModel:  a.argGroup.Model(),
		CmdGroupModel:  a.cmdGroup.Model(nil),
	}
}

func (a *argGroup) Model() *ArgGroupModel {
	m := &ArgGroupModel{}
	for _, arg := range a.args {
		m.Args = append(m.Args, arg.Model())
	}
	return m
}

func (f *flagGroup) Model() *FlagGroupModel {
	m := &FlagGroupModel{}
	for _, fl := range f.flagOrder {
		m.Flags = append(m.Flags, fl.Model())
	}
	return m
}

func (f *Clause) Model() *ClauseModel {
	_, cumulative := f.value.(cumulativeValue)
	return &ClauseModel{
		Name:        f.name,
		Help:        f.help,
		Short:       f.shorthand,
		Default:     f.defaultValues,
		PlaceHolder: f.placeholder,
		Required:    f.required,
		Hidden:      f.hidden,
		Value:       f.value,
		Cumulative:  cumulative,
	}
}

func (c *cmdGroup) Model(parent *CmdModel) *CmdGroupModel {
	m := &CmdGroupModel{}
	for _, cm := range c.commandOrder {
		m.Commands = append(m.Commands, cm.Model(parent))
	}
	return m
}

func (c *CmdClause) Model(parent *CmdModel) *CmdModel {
	depth := 0
	for i := c; i != nil; i = i.parent {
		depth++
	}
	cmd := &CmdModel{
		Name:                c.name,
		Parent:              parent,
		Aliases:             c.aliases,
		Help:                c.help,
		Depth:               depth,
		Hidden:              c.hidden,
		Default:             c.isDefault,
		OptionalSubcommands: c.optionalSubcommands,
		FlagGroupModel:      c.flagGroup.Model(),
		ArgGroupModel:       c.argGroup.Model(),
	}
	cmd.CmdGroupModel = c.cmdGroup.Model(cmd)
	return cmd
}