Reconstruct and merge ordered inputs without general sorting

This commit is contained in:
2026-11-07 12:10:40 +00:00
parent d72c9a788f
commit 7bb7326ed1
4 changed files with 145 additions and 15 deletions

View File

@@ -3,7 +3,6 @@ package commandkit
import (
"errors"
"fmt"
"sort"
"strconv"
commandmeta "go.maximumdirect.net/commandkit/internal/commandmeta"
@@ -298,7 +297,14 @@ func prepareInputWithOccurrences[I any](spec CommandSpec[I], flagValues, flagSpe
fallbackOrdinal++
}
if occurrence.apply != nil {
ordered = append(ordered, occurrence)
if occurrences == nil {
ordered = append(ordered, occurrence)
} else {
if ordered == nil {
ordered = make([]preparedOccurrence[I], len(occurrences))
}
ordered[*occurrence.ordinal] = occurrence
}
}
}
if enforceRequired && binding.meta.Required && !binding.deferredRequired && !value.supplied && missingRequired == "" {
@@ -313,9 +319,12 @@ func prepareInputWithOccurrences[I any](spec CommandSpec[I], flagValues, flagSpe
return preparedInput[I]{}, err
}
preparedValues := append([]preparedValue[I](nil), preparedFlags...)
sort.SliceStable(ordered, func(i, j int) bool { return *ordered[i].ordinal < *ordered[j].ordinal })
// Parsed occurrences use dense parser positions; without a parser log the
// fallback ordinals were assigned in the same order as the append above.
for _, occurrence := range ordered {
preparedValues = append(preparedValues, preparedValue[I]{apply: occurrence.apply})
if occurrence.apply != nil {
preparedValues = append(preparedValues, preparedValue[I]{apply: occurrence.apply})
}
}
for i, argument := range spec.Arguments {
binding := argument.binding