Skip to content

Commit 0e6ac27

Browse files
committed
Add the "Hidden" option for args and opts
To be able to hide args or options from "help" output
1 parent d9d0f2e commit 0e6ac27

4 files changed

Lines changed: 52 additions & 16 deletions

File tree

args.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type BoolArg struct {
2424
HideValue bool
2525
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
2626
SetByUser *bool
27+
// Hide this arg in the help messages
28+
Hidden bool
2729
}
2830

2931
func (a BoolArg) value(into *bool) (flag.Value, *bool) {
@@ -47,6 +49,8 @@ type StringArg struct {
4749
HideValue bool
4850
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
4951
SetByUser *bool
52+
// Hide this arg in the help messages
53+
Hidden bool
5054
}
5155

5256
func (a StringArg) value(into *string) (flag.Value, *string) {
@@ -70,6 +74,8 @@ type IntArg struct {
7074
HideValue bool
7175
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
7276
SetByUser *bool
77+
// Hide this arg in the help messages
78+
Hidden bool
7379
}
7480

7581
func (a IntArg) value(into *int) (flag.Value, *int) {
@@ -93,6 +99,8 @@ type Float64Arg struct {
9399
HideValue bool
94100
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
95101
SetByUser *bool
102+
// Hide this arg in the help messages
103+
Hidden bool
96104
}
97105

98106
func (a Float64Arg) value(into *float64) (flag.Value, *float64) {
@@ -117,6 +125,8 @@ type StringsArg struct {
117125
HideValue bool
118126
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
119127
SetByUser *bool
128+
// Hide this arg in the help messages
129+
Hidden bool
120130
}
121131

122132
func (a StringsArg) value(into *[]string) (flag.Value, *[]string) {
@@ -141,6 +151,8 @@ type IntsArg struct {
141151
HideValue bool
142152
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
143153
SetByUser *bool
154+
// Hide this arg in the help messages
155+
Hidden bool
144156
}
145157

146158
func (a IntsArg) value(into *[]int) (flag.Value, *[]int) {
@@ -165,6 +177,8 @@ type Floats64Arg struct {
165177
HideValue bool
166178
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
167179
SetByUser *bool
180+
// Hide this arg in the help messages
181+
Hidden bool
168182
}
169183

170184
func (a Floats64Arg) value(into *[]float64) (flag.Value, *[]float64) {
@@ -189,6 +203,8 @@ type VarArg struct {
189203
HideValue bool
190204
// Set to true if this arg was set by the user (as opposed to being set from env or not set at all)
191205
SetByUser *bool
206+
// Hide this arg in the help messages
207+
Hidden bool
192208
}
193209

194210
func (a VarArg) value() flag.Value {

commands.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ func (c *Cmd) BoolPtr(into *bool, p BoolParam) {
173173

174174
switch x := p.(type) {
175175
case BoolOpt:
176-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
176+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
177177
case BoolArg:
178-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
178+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
179179
default:
180180
panic(fmt.Sprintf("Unhandled param %v", p))
181181
}
@@ -192,9 +192,9 @@ func (c *Cmd) String(p StringParam) *string {
192192

193193
switch x := p.(type) {
194194
case StringOpt:
195-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
195+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
196196
case StringArg:
197-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
197+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
198198
default:
199199
panic(fmt.Sprintf("Unhandled param %v", p))
200200
}
@@ -232,9 +232,9 @@ func (c *Cmd) Int(p IntParam) *int {
232232

233233
switch x := p.(type) {
234234
case IntOpt:
235-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
235+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
236236
case IntArg:
237-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
237+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
238238
default:
239239
panic(fmt.Sprintf("Unhandled param %v", p))
240240
}
@@ -293,9 +293,9 @@ func (c *Cmd) Float64Ptr(into *float64, p Float64Param) {
293293

294294
switch x := p.(type) {
295295
case Float64Opt:
296-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
296+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
297297
case Float64Arg:
298-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
298+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
299299
default:
300300
panic(fmt.Sprintf("Unhandled param %v", p))
301301
}
@@ -312,9 +312,9 @@ func (c *Cmd) Strings(p StringsParam) *[]string {
312312

313313
switch x := p.(type) {
314314
case StringsOpt:
315-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
315+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
316316
case StringsArg:
317-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
317+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
318318
default:
319319
panic(fmt.Sprintf("Unhandled param %v", p))
320320
}
@@ -352,9 +352,9 @@ func (c *Cmd) Ints(p IntsParam) *[]int {
352352

353353
switch x := p.(type) {
354354
case IntsOpt:
355-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
355+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
356356
case IntsArg:
357-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
357+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
358358
default:
359359
panic(fmt.Sprintf("Unhandled param %v", p))
360360
}
@@ -392,9 +392,9 @@ func (c *Cmd) Floats64(p Floats64Param) *[]float64 {
392392

393393
switch x := p.(type) {
394394
case Floats64Opt:
395-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
395+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
396396
case Floats64Arg:
397-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser})
397+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: value, ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
398398
default:
399399
panic(fmt.Sprintf("Unhandled param %v", p))
400400
}
@@ -431,9 +431,9 @@ Instead, the VarOpt or VarOptArg structs hold the said value.
431431
func (c *Cmd) Var(p VarParam) {
432432
switch x := p.(type) {
433433
case VarOpt:
434-
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: p.value(), ValueSetByUser: x.SetByUser})
434+
c.mkOpt(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: p.value(), ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
435435
case VarArg:
436-
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: p.value(), ValueSetByUser: x.SetByUser})
436+
c.mkArg(container.Container{Name: x.Name, Desc: x.Desc, EnvVar: x.EnvVar, HideValue: x.HideValue, Value: p.value(), ValueSetByUser: x.SetByUser, Hidden: x.Hidden})
437437
default:
438438
panic(fmt.Sprintf("Unhandled param %v", p))
439439
}
@@ -555,6 +555,9 @@ func (c *Cmd) printHelp(longDesc bool) {
555555
fmt.Fprint(w, "\t\nOptions:\t\n")
556556

557557
for _, opt := range c.options {
558+
if opt.Hidden {
559+
continue
560+
}
558561
var (
559562
optNames = formatOptNamesForHelp(opt)
560563
env = formatEnvVarsForHelp(opt.EnvVar)

internal/container/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ type Container struct {
1515
ValueSetByUser *bool
1616
Value flag.Value
1717
DefaultValue string
18+
Hidden bool
1819
}

options.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type BoolOpt struct {
2424
HideValue bool
2525
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
2626
SetByUser *bool
27+
// Hide this option in the help messages
28+
Hidden bool
2729
}
2830

2931
func (o BoolOpt) value(into *bool) (flag.Value, *bool) {
@@ -48,6 +50,8 @@ type StringOpt struct {
4850
HideValue bool
4951
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
5052
SetByUser *bool
53+
// Hide this option in the help messages
54+
Hidden bool
5155
}
5256

5357
func (o StringOpt) value(into *string) (flag.Value, *string) {
@@ -72,6 +76,8 @@ type IntOpt struct {
7276
HideValue bool
7377
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
7478
SetByUser *bool
79+
// Hide this option in the help messages
80+
Hidden bool
7581
}
7682

7783
func (o IntOpt) value(into *int) (flag.Value, *int) {
@@ -96,6 +102,8 @@ type Float64Opt struct {
96102
HideValue bool
97103
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
98104
SetByUser *bool
105+
// Hide this option in the help messages
106+
Hidden bool
99107
}
100108

101109
func (o Float64Opt) value(into *float64) (flag.Value, *float64) {
@@ -121,6 +129,8 @@ type StringsOpt struct {
121129
HideValue bool
122130
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
123131
SetByUser *bool
132+
// Hide this option in the help messages
133+
Hidden bool
124134
}
125135

126136
func (o StringsOpt) value(into *[]string) (flag.Value, *[]string) {
@@ -146,6 +156,8 @@ type IntsOpt struct {
146156
HideValue bool
147157
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
148158
SetByUser *bool
159+
// Hide this option in the help messages
160+
Hidden bool
149161
}
150162

151163
func (o IntsOpt) value(into *[]int) (flag.Value, *[]int) {
@@ -172,6 +184,8 @@ type Floats64Opt struct {
172184
HideValue bool
173185
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
174186
SetByUser *bool
187+
// Hide this option in the help messages
188+
Hidden bool
175189
}
176190

177191
func (o Floats64Opt) value(into *[]float64) (flag.Value, *[]float64) {
@@ -197,6 +211,8 @@ type VarOpt struct {
197211
HideValue bool
198212
// Set to true if this option was set by the user (as opposed to being set from env or not set at all)
199213
SetByUser *bool
214+
// Hide this option in the help messages
215+
Hidden bool
200216
}
201217

202218
func (o VarOpt) value() flag.Value {

0 commit comments

Comments
 (0)