Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions Sources/ArgumentParser/Usage/HelpGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,31 @@ internal struct HelpGenerator {
let description: String

if i < args.count - 1 && args[i + 1].help.keys == arg.help.keys {
// If the next argument has the same keys as this one, output them together
let nextArg = args[i + 1]
// If the next argument has the same keys as this one, we have a group of arguments to output together
var groupedArgs = [arg]
let defaultValue = arg.help.defaultValue.map { "(default: \($0))" } ?? ""
synopsis = "\(arg.synopsisForHelp ?? "")/\(nextArg.synopsisForHelp ?? "")"
description = [arg.help.help?.abstract ?? nextArg.help.help?.abstract, defaultValue]
while i < args.count - 1 && args[i + 1].help.keys == arg.help.keys {
groupedArgs.append(args[i + 1])
i += 1
}

var synopsisString = ""
for arg in groupedArgs {
if !synopsisString.isEmpty { synopsisString.append("/") }
synopsisString.append("\(arg.synopsisForHelp ?? "")")
}
synopsis = synopsisString

var descriptionString: String?
for arg in groupedArgs {
if let desc = arg.help.help?.abstract {
descriptionString = desc
break
}
}
description = [descriptionString, defaultValue]
.compactMap { $0 }
.joined(separator: " ")
i += 1

} else {
let defaultValue = arg.help.defaultValue.flatMap { $0.isEmpty ? nil : "(default: \($0))" } ?? ""
synopsis = arg.synopsisForHelp ?? ""
Expand Down
44 changes: 44 additions & 0 deletions Tests/UnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,48 @@ extension HelpGenerationTests {

""")
}

enum OutputBehaviour: String, CaseIterable { case stats, count, list }
struct E: ParsableCommand {
@Flag(name: .shortAndLong, help: "Change the program output")
var behaviour: OutputBehaviour
}
struct F: ParsableCommand {
@Flag(name: .short, default: .list, help: "Change the program output")
var behaviour: OutputBehaviour
}
struct G: ParsableCommand {
@Flag(inversion: .prefixedNo, help: "Whether to flag")
var flag: Bool
}

func testHelpWithMutuallyExclusiveFlags() {
AssertHelp(for: E.self, equals: """
USAGE: e --stats --count --list

OPTIONS:
-s, --stats/-c, --count/-l, --list
Change the program output
-h, --help Show help information.

""")

AssertHelp(for: F.self, equals: """
USAGE: f [-s] [-c] [-l]

OPTIONS:
-s/-c/-l Change the program output
-h, --help Show help information.

""")

AssertHelp(for: G.self, equals: """
USAGE: g [--flag] [--no-flag]

OPTIONS:
--flag/--no-flag Whether to flag (default: false)
-h, --help Show help information.

""")
}
}