Make bash completion more flexible #1765
Closed
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.
The bash completion, as it is generated by clap, is specific to the
command it is created for. E.g., that is, if it was created for 'myapp'
it can only ever be used for completion on the 'myapp' command. This
constraint is rooted in said command (being provided at build time)
being embedded in the generated code. While it is sensible to enable
completion for the supplied command by default, there should be a way to
make it apply to other commands as well (think, bash aliases, for
example).
Completions for other tools such as git support this out of the box via
an indirection: by providing a function that registers the completion
and that accepts the command to register it for.
With this change we enable this use case for the clap generated bash
completion as well. Instead of hard coding the command name we expect,
we rely on the first argument provided by the complete built-in, which
evaluates to exactly this command (see bash(1); ctrl-f '-F function').
The completion for myapp as it occurs when sourcing the completion
script is unaffected.
With this change, a completion generated for 'myapp' can now be
repurposed to work with a hypothetical alias 'app' as follows:
Fixes #1764
I am not familar with
clap_generate
and I need the fix onv2
, hence, pull request forv2-master
. Change can be crossported tomaster
if desired.