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

custom comp: do not complete flags after args when interspersed is false #1308

Merged
merged 1 commit into from Jul 1, 2021

Conversation

@Luap99
Copy link
Contributor

@Luap99 Luap99 commented Jan 2, 2021

If the interspersed option is set false and one arg is already set all
following arguments are counted as arg and not parsed as flags. Because
of that we should not offer flag completion. The same applies to arguments
followed after --.

Fixes #1307

Copy link
Contributor

@marckhouzam marckhouzam left a comment

Nicely done!

custom_completions.go Outdated Show resolved Hide resolved
custom_completions_test.go Outdated Show resolved Hide resolved
custom_completions_test.go Outdated Show resolved Hide resolved
custom_completions_test.go Outdated Show resolved Hide resolved
custom_completions.go Outdated Show resolved Hide resolved
Copy link
Contributor

@marckhouzam marckhouzam left a comment

This is really nice.
I have mentioned in the comments two more problematic cases, but they are corner cases, especially the one about the args being incomplete.

I don't know if supporting these cases is worth the possible extra complexity to the code, but I haven't tried to fix it myself, so I'm not sure. I personally would not consider them blockers as this PR already improves this scenario for the vast majority of cases.

Thanks for the continuing great work @Luap99 !

custom_completions_test.go Outdated Show resolved Hide resolved
custom_completions_test.go Outdated Show resolved Hide resolved
@Luap99 Luap99 force-pushed the fix-1307 branch 2 times, most recently from ab9afbc to 9805c41 Jan 3, 2021
custom_completions_test.go Outdated Show resolved Hide resolved
childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
return args, ShellCompDirectiveDefault
}
output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--validarg", "")
Copy link
Contributor

@marckhouzam marckhouzam Jan 3, 2021

We have to use a valid flag for this test, which will then trigger the problem:

output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--string", "")

--validarg is not found as a valid flag by checkIfFlagCompletion() so your new logic handling errors is triggered. However, with --string, there will not be an error as it is a valid flag and the --string argument will be trimmed:

trimmedArgs = args[:len(args)-1]

But this is such a rare (and possibly invalid) case that I don't think we should fix it (unless it is a very simple fix).

Copy link
Contributor Author

@Luap99 Luap99 Jan 3, 2021

You are right catching these cases is more difficult and would require a bigger refactor of the checkIfFlagCompletion function.

Copy link
Contributor

@marckhouzam marckhouzam left a comment

This looks good to me. It makes our completion support even more complete (no pun intended 😄 ).

@Luap99
Copy link
Contributor Author

@Luap99 Luap99 commented Feb 18, 2021

I just rebased to solve the merge conflict.

@github-actions
Copy link

@github-actions github-actions bot commented Apr 20, 2021

This PR is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Contributor

@marckhouzam marckhouzam commented May 3, 2021

@jpmcb If you still have energy and time, this is another fix for completion which I had forgotten to mention. This will make the last Fish shell test of the cobra-completion-testing repo work.

@marckhouzam marckhouzam mentioned this pull request May 3, 2021
Copy link
Collaborator

@jpmcb jpmcb left a comment

Hi @Luap99 - getting a number of these PRs in to cut a release. Do you mind rebasing and resolving the conflict?

If the interspersed option is set false and one arg is already set all
following arguments are counted as arg and not parsed as flags. Because
of that we should not offer flag completion. The same applies to arguments
followed after `--`.

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
@Luap99
Copy link
Contributor Author

@Luap99 Luap99 commented Jul 1, 2021

@jpmcb done

jpmcb
jpmcb approved these changes Jul 1, 2021
@jpmcb jpmcb merged commit 5d46ac9 into spf13:master Jul 1, 2021
7 checks passed
@Luap99 Luap99 deleted the fix-1307 branch Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants