Skip to content

Commit 6ca9695

Browse files
committed
add support for hidden flags
1 parent ad6f7de commit 6ca9695

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

command-help-cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func (thisRef *Command) showUsage() {
196196

197197
globalFlagsStarted := false
198198
for i, definedFlag := range flags {
199+
if definedFlag.isHidden == "true" {
200+
continue
201+
}
202+
199203
definedFlag.name = fmt.Sprintf("%"+strconv.Itoa(-longestCommandOrFlagName)+"s", definedFlag.name)
200204
definedFlag.name = " " + definedFlag.name
201205

@@ -299,6 +303,7 @@ func paddedFlags(input []flag) []flag {
299303
isRequired: definedFlagPaddedIsRequired,
300304
defaultValue: definedFlagPaddedDefaultValue,
301305
description: definedFlagPaddedDescription,
306+
isHidden: definedFlag.isHidden,
302307
})
303308
}
304309

flag.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const flagName = "flagName" //
1515
const flagRequired = "flagRequired" // required - needs inpuut from user
1616
const flagDefault = "flagDefault" // default - would be the value if not set
1717
const flagDescription = "flagDescription" //
18+
const flagHidden = "flagHidden" //
1819

1920
type flag struct {
2021
name string
@@ -23,6 +24,7 @@ type flag struct {
2324
defaultValue string
2425
defaultValueWasRequested bool
2526
description string
27+
isHidden string
2628

2729
wasSet bool
2830
}
@@ -52,6 +54,7 @@ func (thisRef *Command) getDefinedFlags() []flag {
5254
isRequired: runtimeStructRef.Type().Field(i).Tag.Get(flagRequired),
5355
defaultValue: runtimeStructRef.Type().Field(i).Tag.Get(flagDefault),
5456
description: runtimeStructRef.Type().Field(i).Tag.Get(flagDescription),
57+
isHidden: runtimeStructRef.Type().Field(i).Tag.Get(flagHidden),
5558
}
5659

5760
if _, tagFound := runtimeStructRef.Type().Field(i).Tag.Lookup(flagDefault); tagFound {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tests
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
8+
clicmdflags "github.com/remoteit/systemkit-clicmdflags"
9+
)
10+
11+
func Test11_CommandWithHiddenFlags_test(t *testing.T) {
12+
args := "help"
13+
os.Args = append(os.Args, strings.Split(args, " ")...)
14+
15+
if err := fiveCmd.Execute(); err != nil {
16+
t.Fatal(err.Error())
17+
}
18+
19+
clicmdflags.DEBUGDumpCommandFlags(fiveCmd)
20+
}

tests/helpers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,22 @@ var fourCmd = &clicmdflags.Command{
102102
clicmdflags.DEBUGDumpCommandFlags(thisCmd)
103103
},
104104
}
105+
106+
// ~~~~ five ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
107+
108+
type fiveCmdFlags struct {
109+
FiveCmdFlags1 bool `flagName:"fiveCmdFlags1" flagRequired:"true" flagHidden:"false" flagDescription:"fiveCmdFlags1 description"`
110+
FiveCmdFlags2 bool `flagName:"fiveCmdFlags2" flagDefault:"false" flagHidden:"true" flagDescription:"fiveCmdFlags2 description"`
111+
FiveCmdFlags3 bool `flagName:"fiveCmdFlags3" flagRequired:"true" flagHidden:"false" flagDescription:"fiveCmdFlags3 description"`
112+
FiveCmdFlags4 bool `flagName:"fiveCmdFlags4" flagDefault:"false" flagHidden:"true" flagDescription:"fiveCmdFlags4 description"`
113+
}
114+
115+
var fiveCmd = &clicmdflags.Command{
116+
Name: "fiveCmd",
117+
Flags: fiveCmdFlags{},
118+
Description: "This is `fiveCmd` description",
119+
Handler: func(thisCmd *clicmdflags.Command) {
120+
fmt.Println("EXEC `fiveCmd`")
121+
clicmdflags.DEBUGDumpCommandFlags(thisCmd)
122+
},
123+
}

0 commit comments

Comments
 (0)