Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for copying ShortDes to LongDesc if it is not present #2694

Merged
merged 1 commit into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ func (c *Command) Subcommand(id string) *Command {
return c.Subcommands[id]
}

type CommandVisitor func(*Command)

// Walks tree of all subcommands (including this one)
func (c *Command) Walk(visitor CommandVisitor) {
visitor(c)
for _, cm := range c.Subcommands {
cm.Walk(visitor)
}
}

func (c *Command) ProcessHelp() {
c.Walk(func(cm *Command) {
ht := &cm.Helptext
if len(ht.LongDescription) == 0 {
ht.LongDescription = ht.ShortDescription
}
})
}

// checkArgValue returns an error if a given arg value is not valid for the
// given Argument
func checkArgValue(v string, found bool, def Argument) error {
Expand Down
39 changes: 39 additions & 0 deletions commands/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,42 @@ func TestResolving(t *testing.T) {
t.Error("Returned command path is different than expected", cmds)
}
}

func TestWalking(t *testing.T) {
cmdA := &Command{
Subcommands: map[string]*Command{
"b": &Command{},
"B": &Command{},
},
}
i := 0
cmdA.Walk(func(c *Command) {
i = i + 1
})
if i != 3 {
t.Error("Command tree walk didn't work, expected 3 got:", i)
}
}

func TestHelpProcessing(t *testing.T) {
cmdB := &Command{
Helptext: HelpText{
ShortDescription: "This is other short",
},
}
cmdA := &Command{
Helptext: HelpText{
ShortDescription: "This is short",
},
Subcommands: map[string]*Command{
"a": cmdB,
},
}
cmdA.ProcessHelp()
if len(cmdA.Helptext.LongDescription) == 0 {
t.Error("LongDescription was not set on basis of ShortDescription")
}
if len(cmdB.Helptext.LongDescription) == 0 {
t.Error("LongDescription was not set on basis of ShortDescription")
}
}
1 change: 1 addition & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ var rootROSubcommands = map[string]*cmds.Command{
}

func init() {
Root.ProcessHelp()
*RootRO = *Root

// sanitize readonly refs command
Expand Down