Skip to content

cmd/root: Replace the usage function with a template - #775

Closed
martymichal wants to merge 1 commit into
containers:mainfrom
martymichal:cmd/root/usage-template
Closed

martymichal wants to merge 1 commit into
containers:mainfrom
martymichal:cmd/root/usage-template

Conversation

@martymichal

Copy link
Copy Markdown
Member

Templates are more dynamic when it comes to context switched. The usage
recognizes as a different context every command/subcommand. Instead of
suggesting:

"Run 'toolbox --help' for usage."

for every command, it makes more sense to suggest:

"Run 'toolbox --help.".

This can be achieved either by setting a specific function for every
command, setting a function that gathers all the necessary info or by
using a template string. This implements the third approach.

The approach causes the loss of specific binary in cases when the binary
has been renamed from "toolbox" to something else because the template
relies on data provided to Cobra, the CLI framework. To me this seems
like an acceptable trade-off because we never allowed renaming of the
binary anyway.

To make use of this in all other command, all lines with:

"Run 'toolbox --help' for usage."

will have to be replaced with cmd.UsageString() or an analogous function
working with the usage string. This is problematic since this requires
access to the cmd variable (cobra.Command) which is not possible in all
cases in cmd/enter.go and cmd/run.go. These functions will have to be
refactored to make use of this.

Templates are more dynamic when it comes to context switched. The usage
recognizes as a different context every command/subcommand. Instead of
suggesting:

  "Run 'toolbox --help' for usage."

for every command, it makes more sense to suggest:

  "Run 'toolbox <command> --help.".

This can be achieved either by setting a specific function for every
command, setting a function that gathers all the necessary info or by
using a template string. This implements the third approach.

The approach causes the loss of specific binary in cases when the binary
has been renamed from "toolbox" to something else because the template
relies on data provided to Cobra, the CLI framework. To me this seems
like an acceptable trade-off because we never allowed renaming of the
binary anyway.

To make use of this in all other command, all lines with:

  "Run 'toolbox --help' for usage."

will have to be replaced with cmd.UsageString() or an analogous function
working with the usage string. This is problematic since this requires
access to the cmd variable (cobra.Command) which is not possible in all
cases in cmd/enter.go and cmd/run.go. These functions will have to be
refactored to make use of this.
@martymichal martymichal added 3. Enhancement Improvement to an existing feature 6. Minor Change Should not cause breakage 2. CLI Issue is related to the command line interface labels May 26, 2021
@martymichal
martymichal requested a review from debarshiray May 26, 2021 16:31
@softwarefactory-delete-me

Copy link
Copy Markdown

Build succeeded.

@martymichal

Copy link
Copy Markdown
Member Author

Before this can be merged some test cases have to be written to make sure the behaviour is correct.

@martymichal

Copy link
Copy Markdown
Member Author

Superseded by #917.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2. CLI Issue is related to the command line interface 3. Enhancement Improvement to an existing feature 6. Minor Change Should not cause breakage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant