Skip to content
Myr

Add Command's RemoveCommand method

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This method removes children commands of an existing command. This allows to build CLI clients that can be extended by 3rd party tools, for instance by adding commands _and replacing the “version” command_. For now the 1st defined command will be executed, so it is not possible to override an existing command. But anyway, deleting old command then adding a new one is the ultimate way to be certain there is no confusion.

authored by Étienne Vallette d'Osia · committed by Étienne Vallette d'Osia · parent f8e1ec5 · Browse files · 1 file+34 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+34 added−0 removed

  • other:+34 added−0 removed

1 file+34 added−0 removed

modified:command.go+34 added
551 unmodified lines
552 c.commands = append(c.commands, x)553 }554}555556// AddCommand removes one or more commands from a parent command.557func (c *Command) RemoveCommand(cmds ...*Command) {558 commands := []*Command{}559main:560 for _, command := range c.commands {561 for _, cmd := range cmds {562 if command == cmd {563 command.parent = nil564 continue main565 }566 }567 commands = append(commands, command)568 }569 c.commands = commands570 // recompute all lengths571 c.commandsMaxUseLen = 0572 c.commandsMaxCommandPathLen = 0573 c.commandsMaxNameLen = 0574 for _, command := range c.commands {575 usageLen := len(command.Use)576 if usageLen > c.commandsMaxUseLen {577 c.commandsMaxUseLen = usageLen578 }579 commandPathLen := len(command.CommandPath())580 if commandPathLen > c.commandsMaxCommandPathLen {581 c.commandsMaxCommandPathLen = commandPathLen582 }583 nameLen := len(command.Name())584 if nameLen > c.commandsMaxNameLen {585 c.commandsMaxNameLen = nameLen586 }587 }588}589590// Convenience method to Print to the defined output591func (c *Command) Print(i ...interface{}) {592 fmt.Fprint(c.Out(), i...)593}
350 unmodified lines