Skip to content

Commit b3a77c4

Browse files
authored
fix for help output of groups of flags (#55)
1 parent 2d256de commit b3a77c4

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,31 @@ internal struct HelpGenerator {
140140
let description: String
141141

142142
if i < args.count - 1 && args[i + 1].help.keys == arg.help.keys {
143-
// If the next argument has the same keys as this one, output them together
144-
let nextArg = args[i + 1]
143+
// If the next argument has the same keys as this one, we have a group of arguments to output together
144+
var groupedArgs = [arg]
145145
let defaultValue = arg.help.defaultValue.map { "(default: \($0))" } ?? ""
146-
synopsis = "\(arg.synopsisForHelp ?? "")/\(nextArg.synopsisForHelp ?? "")"
147-
description = [arg.help.help?.abstract ?? nextArg.help.help?.abstract, defaultValue]
146+
while i < args.count - 1 && args[i + 1].help.keys == arg.help.keys {
147+
groupedArgs.append(args[i + 1])
148+
i += 1
149+
}
150+
151+
var synopsisString = ""
152+
for arg in groupedArgs {
153+
if !synopsisString.isEmpty { synopsisString.append("/") }
154+
synopsisString.append("\(arg.synopsisForHelp ?? "")")
155+
}
156+
synopsis = synopsisString
157+
158+
var descriptionString: String?
159+
for arg in groupedArgs {
160+
if let desc = arg.help.help?.abstract {
161+
descriptionString = desc
162+
break
163+
}
164+
}
165+
description = [descriptionString, defaultValue]
148166
.compactMap { $0 }
149167
.joined(separator: " ")
150-
i += 1
151-
152168
} else {
153169
let defaultValue = arg.help.defaultValue.flatMap { $0.isEmpty ? nil : "(default: \($0))" } ?? ""
154170
synopsis = arg.synopsisForHelp ?? ""

Tests/UnitTests/HelpGenerationTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,48 @@ extension HelpGenerationTests {
124124
125125
""")
126126
}
127+
128+
enum OutputBehaviour: String, CaseIterable { case stats, count, list }
129+
struct E: ParsableCommand {
130+
@Flag(name: .shortAndLong, help: "Change the program output")
131+
var behaviour: OutputBehaviour
132+
}
133+
struct F: ParsableCommand {
134+
@Flag(name: .short, default: .list, help: "Change the program output")
135+
var behaviour: OutputBehaviour
136+
}
137+
struct G: ParsableCommand {
138+
@Flag(inversion: .prefixedNo, help: "Whether to flag")
139+
var flag: Bool
140+
}
141+
142+
func testHelpWithMutuallyExclusiveFlags() {
143+
AssertHelp(for: E.self, equals: """
144+
USAGE: e --stats --count --list
145+
146+
OPTIONS:
147+
-s, --stats/-c, --count/-l, --list
148+
Change the program output
149+
-h, --help Show help information.
150+
151+
""")
152+
153+
AssertHelp(for: F.self, equals: """
154+
USAGE: f [-s] [-c] [-l]
155+
156+
OPTIONS:
157+
-s/-c/-l Change the program output
158+
-h, --help Show help information.
159+
160+
""")
161+
162+
AssertHelp(for: G.self, equals: """
163+
USAGE: g [--flag] [--no-flag]
164+
165+
OPTIONS:
166+
--flag/--no-flag Whether to flag (default: false)
167+
-h, --help Show help information.
168+
169+
""")
170+
}
127171
}

0 commit comments

Comments
 (0)