Preserve reporting flag occurrence order

This commit is contained in:
2026-10-26 02:55:08 +00:00
parent f3bd6dc2ae
commit df21b75f9c
7 changed files with 320 additions and 19 deletions

View File

@@ -1,7 +1,9 @@
package commandkit
import (
"fmt"
"sort"
"strconv"
internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra"
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
@@ -12,7 +14,47 @@ func (a *App) parse(args []string, options commandmeta.ParseOptions) commandmeta
if a == nil {
return commandmeta.Result{Failure: &commandmeta.Failure{Message: "cannot parse commands on a nil app"}}
}
return internalcobra.Parse(a.routeSnapshot().metadata, args, options)
result := internalcobra.Parse(a.routeSnapshot().metadata, args, options)
return reduceReportingOccurrences(result)
}
func reduceReportingOccurrences(result commandmeta.Result) commandmeta.Result {
selection := commandmeta.ReportingSelection{}
for index, occurrence := range result.FlagOccurrences {
if !commandmeta.IsReportingFlag(occurrence.Name) {
continue
}
enabled, err := strconv.ParseBool(occurrence.RawValue)
if err != nil {
result.FlagOccurrences = result.FlagOccurrences[:index+1]
result.Reporting = selection
result.Failure = &commandmeta.Failure{
Message: fmt.Sprintf("invalid boolean value %q for %s", occurrence.RawValue, occurrence.Spelling),
Usage: true,
}
return result
}
mode := commandmeta.ReportingModeNormal
switch occurrence.Name {
case "quiet":
mode = commandmeta.ReportingModeQuiet
case "verbose":
mode = commandmeta.ReportingModeVerbose
case "debug":
mode = commandmeta.ReportingModeDebug
case "progress":
selection.Progress = enabled
selection.ProgressSet = true
continue
}
if enabled {
selection.Mode = mode
} else if selection.Mode == mode {
selection.Mode = commandmeta.ReportingModeNormal
}
}
result.Reporting = selection
return result
}
func (a *App) routeSnapshot() *routingSnapshot {