Improve args passing and DynamicSuggestionsFunc() #6
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.
Args passing
Rather than manipulating
os.Args
(which has a lot of caveats) we configure Cobra to use a specific set of arguments for its parsing of the command being executed.This should work with both existing apps, as well as apps that "wraps" Cobra so its more testable, and thus, calls
root.SetArgs
as part of their bootstrap.I saw behavior where existing implementation when having this library as a
prompt
sub-command never running any of the commands fromroot
despite discovering them and being able to "complete" them in the TUI.Example
main.go
Only used for
go run
andgo build
root.go
Used in testing as well as the regular build (Called from
main.go
)DynamicSuggestionsFunc()
Passing in the
*cobra.Command
toDynamicSuggestionsFunc()
allow you to utilize the(*cobra.Command).ValidArgsFunction()
function to generate the completions.This allow you to provide a generic auto-completion logic based off-of
ValidArgsFunction()
like thisTests
Exciting tests were failing, so I made those pass as well.
Proper shell/args support using
shellquote.Split
strings.Fields
does not account for shell quoting/spacing so in some cases it would provide badArgs()
to Cobra - like if you have a CLI argument that contains a space (e.g.get -n "John Oliver" food apple
) it would instead only see"John
as the CLI flag name, andOliver"
as a part of the argument list.