File: group.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (293 lines) | stat: -rw-r--r-- 5,550 bytes parent folder | download | duplicates (3)
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
package flags

import "fmt"

type GroupModifier func(g *Group)

// Don't display the group name of the flags
func HideGroupName() GroupModifier {
	return func(g *Group) {
		g.showGroupName = false
	}
}

// Don't display this group of flags
func HideGroup() GroupModifier {
	return func(g *Group) {
		g.hidden = true
	}
}

type Group struct {
	flags         []*Flag // flags attached to group
	hidden        bool    // group should not be displayed
	name          string  // name of the group
	set           *Set    // Set group is attached to
	showGroupName bool    // group name should be included in display
}

func newGroup(s *Set, n string, modifiers ...GroupModifier) *Group {
	if s == nil {
		panic("group must be attached to set")
	}
	g := &Group{
		set:           s,
		name:          n,
		flags:         []*Flag{},
		showGroupName: true,
	}

	for _, fn := range modifiers {
		fn(g)
	}

	s.groups = append(s.groups, g)

	return g
}

// Add a flag to the group. This is used to relocate
// a flag from one group to another.
func (g *Group) Add(f *Flag) (err error) {
	if f.group == g {
		return nil
	}

	if f.group != nil {
		idx := -1
		for i, flg := range f.group.flags {
			if flg.longName == f.longName {
				idx = i
				break
			}
		}
		if idx >= 0 {
			f.group.flags = append(f.group.flags[0:idx], f.group.flags[idx+1:]...)
		}
	}

	f.group = g
	g.flags = append(g.flags, f)
	return err
}

// Name of the group
func (g *Group) Name() string {
	return g.name
}

// Flags contained by this group
func (g *Group) Flags() []*Flag {
	f := make([]*Flag, len(g.flags))
	copy(f, g.flags)
	return f
}

// Add a new BooleanType flag
func (g *Group) Bool(
	name string,
	modifiers ...FlagModifier,
) *Flag {
	return newFlag(name, BooleanType, g, modifiers...)
}

// Add a new BooleanType flag using variable
func (g *Group) BoolVar(
	name string,
	ptr *bool,
	modifiers ...FlagModifier,
) *Flag {
	f := g.Bool(name, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Add a new StringType flag
func (g *Group) String(
	name string,
	modifiers ...FlagModifier,
) *Flag {
	return newFlag(name, StringType, g, modifiers...)
}

// Add a new StringType flag using variable
func (g *Group) StringVar(
	name string,
	ptr *string,
	modifiers ...FlagModifier,
) *Flag {
	f := g.String(name, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Add a new ArrayType flag
func (g *Group) Array(
	name string,
	subtype Type,
	modifiers ...FlagModifier,
) *Flag {
	modifiers = append(modifiers, SetSubtype(subtype))
	return newFlag(name, ArrayType, g, modifiers...)
}

// Add a new ArrayType flag using variable
func (g *Group) ArrayVar(
	name string,
	subtype Type,
	ptr interface{},
	modifiers ...FlagModifier,
) *Flag {
	f := g.Array(name, subtype, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Add a new FloatType flag
func (g *Group) Float(
	name string,
	modifiers ...FlagModifier,
) *Flag {
	return newFlag(name, FloatType, g, modifiers...)
}

// Add a new FloatType flag using variable
func (g *Group) FloatVar(
	name string,
	ptr *float64,
	modifiers ...FlagModifier,
) *Flag {
	f := g.Float(name, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Add a new Integer flag
func (g *Group) Integer(
	name string,
	modifiers ...FlagModifier,
) *Flag {
	return newFlag(name, IntegerType, g, modifiers...)
}

// Add a new Integer flag using variable
func (g *Group) IntegerVar(
	name string,
	ptr *int64,
	modifiers ...FlagModifier,
) *Flag {
	f := g.Integer(name, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Add a new IncrementType flag
func (g *Group) Increment(
	name string,
	modifiers ...FlagModifier,
) *Flag {
	modifiers = append(modifiers, SetSubtype(IntegerType))
	return newFlag(name, IncrementType, g, modifiers...)
}

// Add a new IncrementType flag using variable
func (g *Group) IncrementVar(
	name string,
	ptr *int64,
	modifiers ...FlagModifier,
) *Flag {
	f := g.Increment(name, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Add a new MapType flag
func (g *Group) Map(
	name string,
	subtype Type,
	modifiers ...FlagModifier,
) *Flag {
	modifiers = append(modifiers, SetSubtype(subtype))
	return newFlag(name, MapType, g, modifiers...)
}

// Add a new MaptType flag using variable
func (g *Group) MapVar(
	name string,
	subtype Type,
	ptr interface{},
	modifiers ...FlagModifier,
) *Flag {
	f := g.Map(name, subtype, modifiers...)
	f.ptr = true
	f.value = ptr

	return f
}

// Generate printable output of flag group
func (g *Group) Display(
	indent int, // Number of spaces to indent
) string {
	if g.hidden {
		return ""
	}

	var pad int
	opts := []string{}
	desc := []string{}

	for i, f := range g.flags {
		if f.hidden {
			continue
		}
		if f.shortName != 0 {
			opts = append(opts, fmt.Sprintf("-%c,", f.shortName))
		} else {
			opts = append(opts, "   ")
		}
		switch f.kind {
		case BooleanType:
			opts[i] = fmt.Sprintf("%s --[no-]%s", opts[i], f.longName)
		case IncrementType:
			opts[i] = fmt.Sprintf("%s --%s", opts[i], f.longName)
		default:
			opts[i] = fmt.Sprintf("%s --%s VALUE", opts[i], f.longName)
		}
		desc = append(desc, f.description)
		if len(opts[i]) > pad {
			pad = len(opts[i])
		}
	}

	// If there were no flags to display (empty flag collection or all hidden)
	// then just return an empty string
	if len(opts) == 0 {
		return ""
	}

	pad += indent
	var d string

	if g.showGroupName {
		d = fmt.Sprintf("%s:\n", g.name)
	}

	for i := 0; i < len(opts); i++ {
		d = fmt.Sprintf("%s%4s%-*s%s\n", d, "", pad, opts[i], desc[i])
	}

	return d
}