@@ -86,6 +86,11 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
8686
8787 // stop parsing once we see a "--"
8888 if firstArg == "--" {
89+ // In shell completion mode, preserve "--" so that completion can detect
90+ // when the user is completing "--" itself vs. completing after "--"
91+ if cmd .Root ().shellCompletion {
92+ posArgs = append (posArgs , firstArg )
93+ }
8994 posArgs = append (posArgs , rargs [1 :]... )
9095 return & stringSliceArgs {posArgs }, nil
9196 }
@@ -166,6 +171,12 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
166171 // not a bool flag so need to get the next arg
167172 if flagVal == "" && ! valFromEqual {
168173 if len (rargs ) == 1 {
174+ // In shell completion mode, preserve the flag so that DefaultCompleteWithFlags can use it
175+ // as lastArg and offer suggestions for it.
176+ if cmd .Root ().shellCompletion {
177+ posArgs = append (posArgs , rargs ... )
178+ return & stringSliceArgs {posArgs }, nil
179+ }
169180 return & stringSliceArgs {posArgs }, fmt .Errorf ("%s%s" , argumentNotProvidedErrMsg , firstArg )
170181 }
171182 flagVal = rargs [1 ]
@@ -182,6 +193,12 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
182193
183194 // no flag lookup found and short handling is disabled
184195 if ! shortOptionHandling {
196+ // In shell completion mode, preserve the partial flag so that DefaultCompleteWithFlags can use it
197+ // as lastArg and offer suggestions that match the prefix.
198+ if cmd .Root ().shellCompletion {
199+ posArgs = append (posArgs , rargs ... )
200+ return & stringSliceArgs {posArgs }, nil
201+ }
185202 return & stringSliceArgs {posArgs }, fmt .Errorf ("%s%s" , providedButNotDefinedErrMsg , flagName )
186203 }
187204
0 commit comments