Skip to content

Commit 314f8d7

Browse files
committed
fix(argument parser): Return empty when containing empty args
1 parent 90bfd6b commit 314f8d7

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- Only print errors in red if the output terminal supports ANSI colour codes.
1111
[#58](https://github.com/kylef/Commander/pull/58)
1212

13+
- `ArgumentParser.isEmpty` will now return empty for empty arguments.
14+
1315
## 0.8.0
1416

1517
### Enhancements

Sources/Commander/ArgumentParser.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
private enum Arg : CustomStringConvertible {
1+
private enum Arg : CustomStringConvertible, Equatable {
22
/// A positional argument
33
case argument(String)
44

@@ -32,6 +32,11 @@ private enum Arg : CustomStringConvertible {
3232
}
3333

3434

35+
private func == (lhs: Arg, rhs: Arg) -> Bool {
36+
return lhs.description == rhs.description
37+
}
38+
39+
3540
public struct ArgumentParserError : Error, Equatable, CustomStringConvertible {
3641
public let description: String
3742

@@ -77,8 +82,8 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible
7782
return arguments.map { $0.description }.joined(separator: " ")
7883
}
7984

80-
public var isEmpty:Bool {
81-
return arguments.isEmpty
85+
public var isEmpty: Bool {
86+
return arguments.first { $0 != .argument("") } == nil
8287
}
8388

8489
public var remainder:[String] {

Tests/CommanderTests/ArgumentParserSpec.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ public func testArgumentParser() {
1010
parser = ArgumentParser(arguments: ["first", "-f", "--verbose", "middle", "end", "--varOption", "varValue1", "--varOption", "varValue2"])
1111
}
1212

13+
$0.describe("isEmpty") {
14+
$0.it("returns when not empty") {
15+
parser = ArgumentParser(arguments: [])
16+
try expect(parser.isEmpty).to.beTrue()
17+
}
18+
19+
$0.it("returns when not empty") {
20+
try expect(parser.isEmpty).to.beFalse()
21+
}
22+
23+
$0.it("returns empty when containing empty arguments") {
24+
parser = ArgumentParser(arguments: [""])
25+
try expect(parser.isEmpty).to.beTrue()
26+
}
27+
}
28+
1329
$0.describe("shifting") {
1430
$0.it("provides first positional argument") {
1531
try expect(parser.shift()) == "first"

0 commit comments

Comments
 (0)