2026-10-19 19:31:23 +00:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-20 00:29:49 +00:00
|
|
|
"fmt"
|
2026-10-19 19:31:23 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-26 02:55:08 +00:00
|
|
|
func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []commandmeta.FlagAlias) ([]string, map[string][]string, []commandmeta.FlagOccurrence, *commandmeta.Failure) {
|
2026-11-03 03:54:22 +00:00
|
|
|
lookup := commandmeta.NewFlagLookup(flags, aliases)
|
2026-10-19 19:31:23 +00:00
|
|
|
|
|
|
|
|
spellings := make(map[string][]string)
|
2026-10-26 14:37:33 +00:00
|
|
|
var occurrences []commandmeta.FlagOccurrence
|
|
|
|
|
var interpreted []string
|
2026-10-19 19:31:23 +00:00
|
|
|
for index := 0; index < len(argv); {
|
|
|
|
|
token := argv[index]
|
2026-11-03 03:54:22 +00:00
|
|
|
kind := commandmeta.ClassifyFlagToken(token)
|
|
|
|
|
if kind == commandmeta.TerminatorToken {
|
2026-10-19 19:31:23 +00:00
|
|
|
interpreted = append(interpreted, argv[index:]...)
|
|
|
|
|
break
|
|
|
|
|
}
|
2026-11-03 03:54:22 +00:00
|
|
|
if kind == commandmeta.LongFlagToken {
|
|
|
|
|
original, value, attached := commandmeta.SplitLongFlag(token)
|
|
|
|
|
flag, ok := lookup.Find(original)
|
2026-10-19 19:31:23 +00:00
|
|
|
if !ok {
|
|
|
|
|
interpreted = append(interpreted, token)
|
2026-10-26 14:37:33 +00:00
|
|
|
return interpreted, spellings, occurrences, nil
|
2026-10-19 19:31:23 +00:00
|
|
|
}
|
|
|
|
|
spellings[flag.Name] = append(spellings[flag.Name], original)
|
2026-11-03 03:54:22 +00:00
|
|
|
if attached {
|
2026-10-26 02:55:08 +00:00
|
|
|
interpreted = append(interpreted, "--"+flag.Name+"="+value)
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: value})
|
2026-10-19 19:31:23 +00:00
|
|
|
index++
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-10-20 00:28:38 +00:00
|
|
|
if !flag.Boolean && index+1 < len(argv) {
|
2026-10-26 02:55:08 +00:00
|
|
|
value := argv[index+1]
|
|
|
|
|
interpreted = append(interpreted, "--"+flag.Name+"="+value)
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: value})
|
2026-10-19 19:31:23 +00:00
|
|
|
index += 2
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-10-20 00:29:49 +00:00
|
|
|
if !flag.Boolean {
|
2026-10-26 02:55:08 +00:00
|
|
|
return interpreted, spellings, occurrences, &commandmeta.Failure{Message: fmt.Sprintf("flag needs an argument: %s", original)}
|
|
|
|
|
}
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: "true"})
|
2026-10-19 19:31:23 +00:00
|
|
|
interpreted = append(interpreted, "--"+flag.Name)
|
|
|
|
|
index++
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-11-03 03:54:22 +00:00
|
|
|
if kind == commandmeta.ShortFlagToken {
|
2026-10-19 19:31:23 +00:00
|
|
|
consumedNext := false
|
|
|
|
|
for offset := 1; offset < len(token); {
|
|
|
|
|
original := "-" + token[offset:offset+1]
|
2026-11-03 03:54:22 +00:00
|
|
|
flag, ok := lookup.Find(original)
|
2026-10-19 19:31:23 +00:00
|
|
|
if !ok {
|
|
|
|
|
interpreted = append(interpreted, "-"+token[offset:])
|
2026-10-26 14:37:33 +00:00
|
|
|
return interpreted, spellings, occurrences, nil
|
2026-10-19 19:31:23 +00:00
|
|
|
}
|
|
|
|
|
spellings[flag.Name] = append(spellings[flag.Name], original)
|
|
|
|
|
suffix := token[offset+1:]
|
|
|
|
|
if flag.Boolean {
|
|
|
|
|
if strings.HasPrefix(suffix, "=") {
|
2026-10-26 02:55:08 +00:00
|
|
|
value := suffix[1:]
|
|
|
|
|
interpreted = append(interpreted, "--"+flag.Name+"="+value)
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: value})
|
2026-10-19 19:31:23 +00:00
|
|
|
break
|
|
|
|
|
}
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: "true"})
|
2026-10-19 19:31:23 +00:00
|
|
|
interpreted = append(interpreted, "--"+flag.Name)
|
|
|
|
|
offset++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if suffix != "" {
|
|
|
|
|
value := suffix
|
|
|
|
|
if strings.HasPrefix(value, "=") {
|
|
|
|
|
value = value[1:]
|
|
|
|
|
}
|
|
|
|
|
interpreted = append(interpreted, "--"+flag.Name+"="+value)
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: value})
|
2026-10-19 19:31:23 +00:00
|
|
|
break
|
|
|
|
|
}
|
2026-10-20 00:28:38 +00:00
|
|
|
if index+1 < len(argv) {
|
2026-10-26 02:55:08 +00:00
|
|
|
value := argv[index+1]
|
|
|
|
|
interpreted = append(interpreted, "--"+flag.Name+"="+value)
|
2026-10-26 14:37:33 +00:00
|
|
|
occurrences = append(occurrences, commandmeta.FlagOccurrence{Name: flag.Name, Spelling: original, RawValue: value})
|
2026-10-19 19:31:23 +00:00
|
|
|
index += 2
|
|
|
|
|
consumedNext = true
|
|
|
|
|
break
|
|
|
|
|
}
|
2026-10-26 02:55:08 +00:00
|
|
|
return interpreted, spellings, occurrences, &commandmeta.Failure{Message: fmt.Sprintf("flag needs an argument: %s", original)}
|
2026-10-19 19:31:23 +00:00
|
|
|
}
|
|
|
|
|
if !consumedNext {
|
|
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
interpreted = append(interpreted, token)
|
|
|
|
|
index++
|
|
|
|
|
}
|
2026-10-26 02:55:08 +00:00
|
|
|
return interpreted, spellings, occurrences, nil
|
2026-10-19 19:31:23 +00:00
|
|
|
}
|