Skip to content

help: need to simplify help-related code #1136

Closed
@elhimov

Description

@elhimov

Current implementation of help functionality looks quite tricky.

First, brief description of the ways Cobra provides to customize help behavior. There are multiple of them and they can be used in any combination:

  • Custom help command:
    By default Cobra automatically generates 'help' command so we can launch tt help [CMD [SUBCMD]] w/o any additional efforts (we don't need to create 'help' command). Being executed the default 'help' command just find corresponding command object and call its help function. Can be overridden with cmd.setHelpCommand(customHelpCmd) (usually it is overridded for the root command because 'help' is expected to be launched as top-level command).

  • Custom help function:
    By default Cobra uses command help template to generate help string out of command object. Can be overridden for the certain command with cmd.setHelpFunc(customHelpFunc).

  • Custom help template:
    By default Cobra obtains template from the parent command if any or use the predefined one. Usually single template is defined for the root command and it is used with all commands. Can be overridden with cmd.setHelpTemplate(customHelpTemplate).

  • Custom usage func/template:
    Used by default help func/template and work similar. They don't bring any new concept and are mentioned just for the reference.

Problem items:

  1. Considering that Cobra doesn't provide API to get help command it turns out that there are only 2 "legal" approaches: we should either use the default help command as-is or define a custom one with cmd.setHelpCommand(customHelpCmd) if the default one doesn't meet our expectations. However current implementation follows the other approach:

    • Introduce hack-function that implements "the missed" functionality (util.GetHelpCommand obtain help command using the knowledge of Cobra's internals)
    • Create default 'help' command with rootCmd.InitDefaultHelpCmd() (usually we should not know about this function because Cobra calls it internally in a proper place, but the mentioned internals-knowledge forces us to call it before util.GetHelpCommand to make it work)
    • Get that default 'help' command and do some customization

    Customization in a such strange way looks like an attempt to reuse functionality of the default help command at any cost but I doubt that it worth it.

  2. Handling of variation between internal and external help command has its own implementation though there is nothing special in it comparing to the other commands and common code could be reused.

  3. There is duplication of Cobra's functionality that parses help flags.

Suggested solution:

Consider implementation of custom help command in a way similar to the other commands. It will allow both:

  • use "legal" approach and thus get rid of util.GetHelpCommand hack
  • reuse common mechanism of handling variation between internal and external cases and thus reduce code duplication

Consider implementation of getting help for external command over cmd.Help() so help command could handle external and internal commands uniformly. It will allow both:

  • simplify implementation of custom help command
  • rely on internal Cobra help flags parsing and thus get rid of unnecessary custom parsing

Metadata

Metadata

Assignees

Labels

code healthImprove code readability, simplify maintenance and so on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions