Retain entered flag spellings in parser failures

This commit is contained in:
2026-10-20 00:29:49 +00:00
parent eb92f7be7f
commit 520d7e125b
5 changed files with 108 additions and 8 deletions

View File

@@ -1,12 +1,13 @@
package cobra
import (
"fmt"
"strings"
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
)
func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []commandmeta.FlagAlias) ([]string, map[string][]string) {
func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []commandmeta.FlagAlias) ([]string, map[string][]string, *commandmeta.Failure) {
canonical := make(map[string]commandmeta.Flag, len(flags)*2+2)
help := commandmeta.Flag{Name: "help", Short: "h", Boolean: true}
canonical["--help"] = help
@@ -61,6 +62,9 @@ func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []comm
index += 2
continue
}
if !flag.Boolean {
return interpreted, spellings, &commandmeta.Failure{Message: fmt.Sprintf("flag needs an argument: %s", original)}
}
interpreted = append(interpreted, "--"+flag.Name)
index++
continue
@@ -107,8 +111,7 @@ func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []comm
consumedNext = true
break
}
interpreted = append(interpreted, "--"+flag.Name)
break
return interpreted, spellings, &commandmeta.Failure{Message: fmt.Sprintf("flag needs an argument: %s", original)}
}
if !consumedNext {
index++
@@ -118,5 +121,5 @@ func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []comm
interpreted = append(interpreted, token)
index++
}
return interpreted, spellings
return interpreted, spellings, nil
}