in Sources/ArgumentParser/Parsing/SplitArguments.swift [439:476]
mutating func remove(at position: Index) {
guard !isEmpty else { return }
// Find the first element at the given input index. Since `elements` is
// always sorted by input index, we can leave this method if we see a
// higher value than `position`.
var start = elements.startIndex
while start < elements.endIndex {
if elements[start].index.inputIndex == position.inputIndex { break }
if elements[start].index.inputIndex > position.inputIndex { return }
start += 1
}
guard start < elements.endIndex else { return }
if case .complete = position.subIndex {
// When removing a `.complete` position, we need to remove both the
// complete element and any sub-elements with the same input index.
// Remove up to the first element where the input index doesn't match.
let end = elements[start...].firstIndex(where: { $0.index.inputIndex != position.inputIndex })
?? elements.endIndex
remove(subrange: start..<end)
} else {
// When removing a `.sub` (i.e. non-`.complete`) position, we need to
// also remove the `.complete` position, if it exists. Since `.complete`
// positions always come before sub-positions, if one exists it will be
// the position found as `start`.
if elements[start].index.subIndex == .complete {
remove(at: start)
start += 1
}
if let sub = elements[start...].firstIndex(where: { $0.index == position }) {
remove(at: sub)
}
}
}