Skip to content

Commit

Permalink
Fixed usage synopsis when alwaysCompactUsageOptions is enabled and …
Browse files Browse the repository at this point in the history
…options are all optional or all non-optional (or when list of options is too long)
  • Loading branch information
YourMJK committed Jul 11, 2023
1 parent 3bf4005 commit 4a52de0
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Sources/ArgumentParser/Usage/UsageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,32 @@ extension UsageGenerator {
/// In `roff`.
var synopsis: String {
var options = Array(definition)
if options.count == 0 {
return toolName
}
else if options.count > 12 || compactUsageOptions {
var components = [toolName]
if options.count > 12 || compactUsageOptions {
// When we have too many options, keep required and positional arguments,
// but discard the rest.
var optionalOptionsCount = options.count
options = options.filter {
$0.isPositional || !$0.help.options.contains(.isOptional)
}
optionalOptionsCount -= options.count
// If there are between 1 and 12 options left, print them, otherwise print
// a simplified usage string.
if !options.isEmpty, options.count <= 12 {
let synopsis = options
.map { $0.synopsis }
.joined(separator: " ")
return "\(toolName) [OPTIONS ...] \(synopsis)"
if options.count > 12 {
components.append("<OPTIONS/ARGUMENTS ...>")
options.removeAll()
}
else if optionalOptionsCount != 0 {
components.append("[OPTIONS ...]")
}
return "\(toolName) OPTIONS ..."
}
else {
if !options.isEmpty {
let synopsis = options
.map { $0.synopsis }
.joined(separator: " ")
return "\(toolName) \(synopsis)"
components.append(synopsis)
}
return components.joined(separator: " ")
}
}

Expand Down

0 comments on commit 4a52de0

Please sign in to comment.