Skip to content

Commit b34d67f

Browse files
committed
Refactor argument name extraction to eliminate warning
Replaced multiple `filter(...).first` calls with a single `compactMap(...).first` to simplify logic and eliminate compiler warnings related to unwrapped optionals and unused bindings. The new implementation is more concise and performs better.
1 parent ed0160e commit b34d67f

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Tools/generate-command-models/GenerateCommandModels.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,12 @@ struct GenerateCommandModels: AsyncParsableCommand {
176176

177177
private func argSwitchCase(_ arg: ArgumentInfoV0) -> String {
178178
let flag = arg.kind == .flag
179-
var name: String?
180-
if let longName = arg.names?.filter { $0.kind == .long }.first?.name {
181-
name = "--" + longName
182-
} else if let shortName = arg.names?.filter { $0.kind == .short }.first?.name {
183-
name = "-" + shortName
184-
} else if let longNameWithSingleDash = arg.names?.filter { $0.kind == .longWithSingleDash }.first?.name {
185-
name = "-" + longNameWithSingleDash
186-
}
179+
let name: String? = arg.names?.compactMap { name in
180+
switch name.kind {
181+
case .long: return "--" + name.name
182+
case .short, .longWithSingleDash: return "-" + name.name
183+
}
184+
}.first
187185

188186
guard let name else { fatalError("Unable to find a suitable argument name for \(arg)") }
189187

0 commit comments

Comments
 (0)