cmd/root: Replace the usage function with a template #775
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.