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
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public struct NameSpecification: ExpressibleByArrayLiteral {
/// Short labels can be combined into groups.
case customShort(Character)
}
var elements: Set<Element>
var elements: Array<Element>

public init<S>(_ sequence: S) where S : Sequence, Element == S.Element {
self.elements = Set(sequence)
self.elements = sequence.uniquified()
}

public init(arrayLiteral elements: Element...) {
Expand Down
9 changes: 9 additions & 0 deletions Sources/ArgumentParser/Parsing/Name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ extension Name {
return n
}
}

var isShort: Bool {
switch self {
case .short:
return true
default:
return false
}
}
}

// short argument names based on the synopsisString
Expand Down
26 changes: 4 additions & 22 deletions Sources/ArgumentParser/Usage/UsageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension ArgumentDefinition {

switch kind {
case .named:
let joinedSynopsisString = sortedNames
let joinedSynopsisString = partitionedNames
.map { $0.synopsisString }
.joined(separator: ", ")

Expand Down Expand Up @@ -113,30 +113,12 @@ extension ArgumentDefinition {
return unadornedSynopsis
}

var sortedNames: [Name] {
return names
.sorted { (lhs, rhs) -> Bool in
switch (lhs, rhs) {
case let (.long(l), .long(r)):
return l < r
case (_, .long):
return true
case (.long, _):
return false
case let (.short(l), .short(r)):
return l < r
case (_, .short):
return true
case (.short, _):
return false
case let (.longWithSingleDash(l), .longWithSingleDash(r)):
return l < r
}
}
var partitionedNames: [Name] {
return names.filter{ $0.isShort } + names.filter{ !$0.isShort }
}

var preferredNameForSynopsis: Name? {
sortedNames.last
names.first{ !$0.isShort } ?? names.first
}

var synopsisValueName: String? {
Expand Down
22 changes: 22 additions & 0 deletions Sources/ArgumentParser/Utilities/SequenceExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension Sequence where Element: Equatable {
func uniquified() -> [Element] {
var sequence = Array<Element>()
for element in self {
if !sequence.contains(element) {
sequence.append(element)
}
}
return sequence
}
}
19 changes: 19 additions & 0 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,23 @@ extension HelpGenerationTests {

""")
}

struct L: ParsableArguments {
@Option(
name: [.short, .customLong("remote"), .customLong("remote"), .short, .customLong("when"), .long, .customLong("other", withSingleDash: true), .customLong("there"), .customShort("x"), .customShort("y")],
help: "Help Message")
var time: String?
}

func testHelpWithMultipleCustomNames() {
AssertHelp(for: L.self, equals: """
USAGE: l [--remote <remote>]

OPTIONS:
-t, -x, -y, --remote, --when, --time, -other, --there <remote>
Help Message
-h, --help Show help information.

""")
}
}
24 changes: 24 additions & 0 deletions Tests/ArgumentParserUnitTests/UsageGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,28 @@ extension UsageGenerationTests {
let help = UsageGenerator(toolName: "bar", parsable: J())
XCTAssertEqual(help.synopsis, "bar --req <req> [--opt <opt>]")
}

struct K: ParsableArguments {
@Option(
name: [.short, .customLong("remote"), .customLong("when"), .customLong("there")],
help: "Help Message")
var time: String?
}

func testSynopsisWithMultipleCustomNames() {
let help = UsageGenerator(toolName: "bar", parsable: K())
XCTAssertEqual(help.synopsis, "bar [--remote <remote>]")
}

struct L: ParsableArguments {
@Option(
name: [.short, .short, .customLong("remote", withSingleDash: true), .short, .customLong("remote", withSingleDash: true)],
help: "Help Message")
var time: String?
}

func testSynopsisWithSingleDashLongNameFirst() {
let help = UsageGenerator(toolName: "bar", parsable: L())
XCTAssertEqual(help.synopsis, "bar [-remote <remote>]")
}
}