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

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

Closed

Conversation

HarryMichal
Copy link
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.
@HarryMichal HarryMichal 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
@softwarefactory-project-zuul
Copy link

Build succeeded.

@HarryMichal
Copy link
Member Author

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

@HarryMichal
Copy link
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