Parse invocation flags across command discovery

This commit is contained in:
2026-11-22 20:53:25 +00:00
parent ea3ce25001
commit b6cb183a71
10 changed files with 136 additions and 30 deletions

View File

@@ -77,7 +77,7 @@ func (session *Session) Parse(argv []string) commandmeta.Result {
}
remaining := append(commandpath.CloneTokens(unconsumed), tail...)
localFlags := commandmeta.EffectiveFlags(selectedNode.Flags, selectedNode.CoreFlags)
localFlags := commandmeta.EffectiveFlags(append(append([]commandmeta.Flag(nil), selectedNode.Flags...), selectedNode.InvocationFlags...), selectedNode.CoreFlags)
aliases := session.flagAliases
if session.flagAliasesForPath != nil {
aliases = session.flagAliasesForPath(result.Path)
@@ -115,6 +115,15 @@ func (session *Session) Parse(argv []string) commandmeta.Result {
result.FlagSpellings[index] = raw.spellingsCopy()
}
}
result.InvocationFlagValues = make([][]string, len(selectedNode.InvocationFlags))
result.InvocationFlagSpellings = make([][]string, len(selectedNode.InvocationFlags))
for index, flag := range selectedNode.InvocationFlags {
if value := selected.Flags().Lookup(flag.Name); value != nil {
raw := value.Value.(*rawValue)
result.InvocationFlagValues[index] = raw.valuesCopy()
result.InvocationFlagSpellings[index] = raw.spellingsCopy()
}
}
if helpFlag := selected.Flags().Lookup("help"); helpFlag != nil {
helpRequested, err := helpFlag.Value.(*rawValue).lastBool()
if err != nil {
@@ -271,7 +280,7 @@ func (session *Session) UsageLine(path []string) (string, *commandmeta.Failure)
if flag.Name != "debug" || !flag.Boolean {
continue
}
for _, effective := range commandmeta.EffectiveFlags(declaration.Flags, declaration.CoreFlags) {
for _, effective := range commandmeta.EffectiveFlags(append(append([]commandmeta.Flag(nil), declaration.Flags...), declaration.InvocationFlags...), declaration.CoreFlags) {
if effective.Name == "debug" {
spelling := "--debug"
if effective.Short != "" {
@@ -330,7 +339,7 @@ func buildTree(root *commandmeta.Command) (*spfcobra.Command, map[*spfcobra.Comm
helpFlag := backend.Flags().Lookup("help")
helpFlag.NoOptDefVal = "true"
helpFlag.Usage = "help for this command (default: false)"
for _, declarationFlag := range commandmeta.EffectiveFlags(declaration.Flags, declaration.CoreFlags) {
for _, declarationFlag := range commandmeta.EffectiveFlags(append(append([]commandmeta.Flag(nil), declaration.Flags...), declaration.InvocationFlags...), declaration.CoreFlags) {
value := &rawValue{boolean: declarationFlag.Boolean}
backend.Flags().VarP(value, declarationFlag.Name, declarationFlag.Short, flagUsage(declarationFlag))
backendFlag := backend.Flags().Lookup(declarationFlag.Name)

View File

@@ -16,9 +16,10 @@ type Command struct {
Runnable bool
Flags []Flag
// CoreFlags are parsed by CommandKit but do not occupy consumer binding indexes.
CoreFlags []Flag
Arguments []Argument
Children []*Command
CoreFlags []Flag
InvocationFlags []Flag
Arguments []Argument
Children []*Command
}
// Argument describes one positional input for usage rendering.
@@ -196,15 +197,17 @@ type ReportingSelection struct {
// Result contains the selected command and raw input collected by the adapter.
type Result struct {
Key Key
Path []string
HelpRequested bool
FlagValues [][]string
FlagSpellings [][]string
FlagOccurrences []FlagOccurrence
Reporting ReportingSelection
Operands []string
Failure *Failure
Key Key
Path []string
HelpRequested bool
FlagValues [][]string
FlagSpellings [][]string
InvocationFlagValues [][]string
InvocationFlagSpellings [][]string
FlagOccurrences []FlagOccurrence
Reporting ReportingSelection
Operands []string
Failure *Failure
}
// ParseOptions selects flag aliases for parser contexts.