@cryptotaxi247 / kubo / commits / 7f61b3170

Add logic for copying ShortDes to LongDesc if it is not present This is the best place for inserting it that I found.

Add logic for copying ShortDes to LongDesc if it is not present This is the best place for inserting it that I found. Test in #2648 should be modified to run `Root.ProcessHelp()`. License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed May 14, 2016 at 12:56 UTC 7f61b3170fa12562fb24853c8a9883a53cb4dd5c
3 files changed +59
commands/command.go
+19
@@ -258,6 +258,25 @@ func (c *Command) Subcommand(id string) *Command {
258 return c.Subcommands[id]
259 }
260
261 +type CommandVisitor func(*Command)
262 +
263 +// Walks tree of all subcommands (including this one)
264 +func (c *Command) Walk(visitor CommandVisitor) {
265 + visitor(c)
266 + for _, cm := range c.Subcommands {
267 + cm.Walk(visitor)
268 + }
269 +}
270 +
271 +func (c *Command) ProcessHelp() {
272 + c.Walk(func(cm *Command) {
273 + ht := &cm.Helptext
274 + if len(ht.LongDescription) == 0 {
275 + ht.LongDescription = ht.ShortDescription
276 + }
277 + })
278 +}
279 +
280 // checkArgValue returns an error if a given arg value is not valid for the
281 // given Argument
282 func checkArgValue(v string, found bool, def Argument) error {
commands/command_test.go
+39
@@ -155,3 +155,42 @@ func TestResolving(t *testing.T) {
155 t.Error("Returned command path is different than expected", cmds)
156 }
157 }
158 +
159 +func TestWalking(t *testing.T) {
160 + cmdA := &Command{
161 + Subcommands: map[string]*Command{
162 + "b": &Command{},
163 + "B": &Command{},
164 + },
165 + }
166 + i := 0
167 + cmdA.Walk(func(c *Command) {
168 + i = i + 1
169 + })
170 + if i != 3 {
171 + t.Error("Command tree walk didn't work, expected 3 got:", i)
172 + }
173 +}
174 +
175 +func TestHelpProcessing(t *testing.T) {
176 + cmdB := &Command{
177 + Helptext: HelpText{
178 + ShortDescription: "This is other short",
179 + },
180 + }
181 + cmdA := &Command{
182 + Helptext: HelpText{
183 + ShortDescription: "This is short",
184 + },
185 + Subcommands: map[string]*Command{
186 + "a": cmdB,
187 + },
188 + }
189 + cmdA.ProcessHelp()
190 + if len(cmdA.Helptext.LongDescription) == 0 {
191 + t.Error("LongDescription was not set on basis of ShortDescription")
192 + }
193 + if len(cmdB.Helptext.LongDescription) == 0 {
194 + t.Error("LongDescription was not set on basis of ShortDescription")
195 + }
196 +}
core/commands/root.go
+1
@@ -153,6 +153,7 @@ var rootROSubcommands = map[string]*cmds.Command{
153 }
154
155 func init() {
156 + Root.ProcessHelp()
157 *RootRO = *Root
158
159 // sanitize readonly refs command