Skip to content

Commit afeb200

Browse files
authored
Fix short names with equals sign (#141)
If an argument has a single dash and single character, followed by a value, treat it as a short name. `-c=1` -> `Name.short("c")` Otherwise, treat it as a long name with single dash. `-count=1` -> `Name.longWithSingleDash("count")`
1 parent 501bf60 commit afeb200

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

Sources/ArgumentParser/Parsing/SplitArguments.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,14 @@ private extension ParsedArgument {
493493
}
494494

495495
init(longArgWithSingleDashRemainder remainder: Substring) throws {
496-
try self.init(longArgRemainder: remainder, makeName: { Name.longWithSingleDash(String($0)) })
496+
try self.init(longArgRemainder: remainder, makeName: {
497+
/// If an argument has a single dash and single character,
498+
/// followed by a value, treat it as a short name.
499+
/// `-c=1` -> `Name.short("c")`
500+
/// Otherwise, treat it as a long name with single dash.
501+
/// `-count=1` -> `Name.longWithSingleDash("count")`
502+
$0.count == 1 ? Name.short($0.first!) : Name.longWithSingleDash(String($0))
503+
})
497504
}
498505

499506
init(longArgRemainder remainder: Substring, makeName: (Substring) -> Name) throws {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===----------------------------------------------------------*- swift -*-===//
2+
//
3+
// This source file is part of the Swift Argument Parser open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import XCTest
13+
import ArgumentParserTestHelpers
14+
import ArgumentParser
15+
16+
final class EqualsEndToEndTests: XCTestCase {
17+
}
18+
19+
// MARK: .short name
20+
21+
fileprivate struct Foo: ParsableArguments {
22+
@Flag(name: .short) var toggle: Bool
23+
@Option(name: .short) var name: String?
24+
@Option(name: .short) var format: String
25+
}
26+
27+
extension EqualsEndToEndTests {
28+
func testEquals_withShortName() throws {
29+
AssertParse(Foo.self, ["-n=Name", "-f=Format"]) { foo in
30+
XCTAssertEqual(foo.toggle, false)
31+
XCTAssertEqual(foo.name, "Name")
32+
XCTAssertEqual(foo.format, "Format")
33+
}
34+
}
35+
36+
func testEquals_withCombinedShortName_1() throws {
37+
AssertParse(Foo.self, ["-tf", "Format"]) { foo in
38+
XCTAssertEqual(foo.toggle, true)
39+
XCTAssertEqual(foo.name, nil)
40+
XCTAssertEqual(foo.format, "Format")
41+
}
42+
}
43+
44+
func testEquals_withCombinedShortName_2() throws {
45+
XCTAssertThrowsError(try Foo.parse(["-tf=Format"]))
46+
}
47+
}
48+
49+
// MARK: .shortAndLong name
50+
51+
fileprivate struct Bar: ParsableArguments {
52+
@Option(name: .shortAndLong) var name: String
53+
@Option(name: .shortAndLong) var format: String
54+
}
55+
56+
extension EqualsEndToEndTests {
57+
func testEquals_withShortAndLongName() throws {
58+
AssertParse(Bar.self, ["-n=Name", "-f=Format"]) { bar in
59+
XCTAssertEqual(bar.name, "Name")
60+
XCTAssertEqual(bar.format, "Format")
61+
}
62+
}
63+
}
64+
65+
// MARK: .customShort name
66+
67+
fileprivate struct Baz: ParsableArguments {
68+
@Option(name: .customShort("i")) var name: String
69+
@Option(name: .customShort("t")) var format: String
70+
}
71+
72+
extension EqualsEndToEndTests {
73+
func testEquals_withCustomShortName() throws {
74+
AssertParse(Baz.self, ["-i=Name", "-t=Format"]) { baz in
75+
XCTAssertEqual(baz.name, "Name")
76+
XCTAssertEqual(baz.format, "Format")
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)