-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 autocompletion after double dash (--
)
#1877
Comments
Thanks @richard-kramer for the report. Line 294 in 9235920
we are modifying the state of the real -- counter and that value is kept. We should either reset the value or use a dummy flagset to avoid this.
cc @Luap99 |
see 5d46ac9 If we add a
That would also be much better since we only would parse once and avoid other potential side effects like this one. |
Do you know any workaround to accomplish this, if pflag cannot be changed? I'm actually a bit at a loss, because I think I tried to work with all options I have access to, but I might also be overlooking something and am not ready to give up yet. |
The ugly workaround would be to parse the args all by yourself. You could check if os.Args contains In any case someone should open a PR with such change against this repo. Maybe some of the active maintainers here have some know how to get stuff merged there? Otherwise as @marckhouzam said we could fix it here by using a dummy flag set for this check. |
Oops, I was absolutely not aware of Current workaround using // cmd/test.go
// …
import (
// …
"os"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
// …
)
var validArgs = []string{"foo", "bar", "baz"}
var TestCmd = &cobra.Command{
// …
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
possibleArgs := append(validArgs, "--")
if !slices.Contains(os.Args, "--") {
return possibleArgs, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveDefault
}
// …
}
// … |
The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:
|
I'm currently trying out the cobra framework and stumbled upon an issue with dynamic autocompletion and
cmd.ArgsLenAtDash()
.I'm trying to provide autocompletion for a predefined set of possible positional parameters until a
--
is added. Everything after that should be passed down to another command (or otherwise processed differently to allargs
before the double dash) and should provide different autocompletion results.Unfortunately, because of the added
--
here, the result ofcmd.ArgsLenAtDash()
insideValidArgsFunction
is always at least0
, even when there is no--
in my command. This makes it impossible to detect, if a--
hasn't been added to the arguments yet. As soon as I add another argumentbehind
the--
,cmd.ArgsLenAtDash()
returns the correct result.Now I'm not sure, if this is simply not a supported use case, if I'm missing something or if this is actually a bug.
Example Code
This is an example
cmd/test.go
file inside an initialized cobra project, added to the root command.I'm expecting to get completions like these:
But I'm always getting the last return values:
My goal is to add different autocompletion for possible arguments after the
--
, but the only issue with that is the correct detection of when the--
has been added.The text was updated successfully, but these errors were encountered: