Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Fix hidden command usage (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmengin authored and mmatur committed Oct 17, 2018
1 parent cd1d1cd commit 9c0eb9a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
14 changes: 11 additions & 3 deletions flaeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ func LoadWithCommand(cmd *Command, cmdArgs []string, customParsers map[reflect.T

// PrintHelpWithCommand generates and prints command line help for a Command
func PrintHelpWithCommand(flagMap map[string]reflect.StructField, defaultValMap map[string]reflect.Value, parsers map[reflect.Type]parse.Parser, cmd *Command, subCmd []*Command) error {
// Hide command from help
if cmd != nil && cmd.HideHelp {
return fmt.Errorf("command %s not found", cmd.Name)
}

// Define a templates
// Using POSXE STD : http://pubs.opengroup.org/onlinepubs/9699919799/
const helper = `{{if .ProgDescription}}{{.ProgDescription}}
Expand All @@ -457,7 +462,7 @@ Flags:
SubCommands map[string]string
}
tempStruct := TempStruct{}
if cmd != nil && !cmd.HideHelp {
if cmd != nil {
tempStruct.ProgName = cmd.Name
tempStruct.ProgDescription = cmd.Description
tempStruct.SubCommands = map[string]string{}
Expand Down Expand Up @@ -582,7 +587,10 @@ func PrintErrorWithCommand(err error, flagMap map[string]reflect.StructField, de
fmt.Printf("Error here : %s\n", err)
}

PrintHelpWithCommand(flagMap, defaultValMap, parsers, cmd, subCmd)
if errHelp := PrintHelpWithCommand(flagMap, defaultValMap, parsers, cmd, subCmd); errHelp != nil {
return errHelp
}

return err
}

Expand Down Expand Up @@ -661,7 +669,7 @@ func (f *Flaeg) findCommandWithCommandArgs() (*Command, []string, error) {
commandName, f.commandArgs = splitArgs(f.args)
if len(commandName) > 0 {
for _, command := range f.commands {
if commandName == command.Name && !command.HideHelp {
if commandName == command.Name {
f.calledCommand = command
return f.calledCommand, f.commandArgs, nil
}
Expand Down
62 changes: 61 additions & 1 deletion flaeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,66 @@ Complete documentation is available at https://github.com/containous/flaeg`,
}

func TestCommandHidden(t *testing.T) {
// INIT
args := []string{
"--help",
}

// hidden root command
hiddenDescription := "The Hidden root command"
versionConfig := &VersionConfig{Version: "0.1"}
rootCmd := &Command{
Name: "secret",
Description: hiddenDescription,
Config: versionConfig,
DefaultPointersConfig: versionConfig,
HideHelp: true,
Run: func() error {
return nil
},
}

// TEST
// init flaeg
flaeg := New(rootCmd, args)
// add custom parser to flaeg
flaeg.AddParser(reflect.TypeOf([]ServerInfo{}), &sliceServerValue{})

// catch stdout
backupStdout := os.Stdout
defer func() {
os.Stdout = backupStdout
}()
r, w, _ := os.Pipe()
os.Stdout = w

// run test
expectedErrMessage := "command secret not found"
if err := flaeg.Run(); err == nil || err.Error() != expectedErrMessage {
t.Errorf("Expected Error: %q , actual: %v", expectedErrMessage, err)
}

// read and restore stdout
err := w.Close()
if err != nil {
t.Fatal(err)
}
out, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}

os.Stdout = backupStdout

output := string(out)
fmt.Println(output)

if strings.Contains(output, hiddenDescription) {
t.Errorf("The command must be hidden in the console: %s", output)
}
}

func TestSubCommandHidden(t *testing.T) {
// INIT
rootConfig := newConfiguration()
rootDefaultPointers := newDefaultPointersConfiguration()
Expand Down Expand Up @@ -2424,7 +2484,7 @@ Complete documentation is available at https://github.com/containous/flaeg`,
fmt.Println(output)

if strings.Contains(output, hiddenDescription) {
t.Errorf("The command must be hidden is th console: %s", output)
t.Errorf("The command must be hidden in the console: %s", output)
}
}

Expand Down

0 comments on commit 9c0eb9a

Please sign in to comment.