1
0
Fork 0
mirror of https://github.com/spf13/cobra synced 2025-04-01 20:39:12 +00:00

fix: Prevent getCompletions from modifying os.Args

This commit is contained in:
Jonathan Conder 2025-03-12 13:30:46 +13:00
parent ceb39aba25
commit 1b401ede18
No known key found for this signature in database
GPG key ID: 62C458884CAF799E

View file

@ -359,7 +359,9 @@ func (c *Command) getCompletions(args []string) (*Command, []Completion, ShellCo
// if -- was already set or interspersed is false and there is already one arg then
// the extra added -- is counted as arg.
flagCompletion := true
_ = finalCmd.ParseFlags(append(finalArgs, "--"))
extendedArgs := make([]string, 0, len(finalArgs)+1)
extendedArgs = append(append(extendedArgs, finalArgs...), "--")
_ = finalCmd.ParseFlags(extendedArgs)
newArgCount := finalCmd.Flags().NArg()
// Parse the flags early so we can check if required flags are set