Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support positional arguments #75

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 14 additions & 1 deletion Sources/Commander/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible

/// Initialises the ArgumentParser with an array of arguments
public init(arguments: [String]) {
self.arguments = arguments.map { argument in
let splitArguments = arguments.split(maxSplits: 1, omittingEmptySubsequences: false) { $0 == "--" }

let unfixedArguments: [String]
let fixedArguments: [String]
if splitArguments.count == 2, let prefix = splitArguments.first, let suffix = splitArguments.last {
unfixedArguments = Array(prefix)
fixedArguments = Array(suffix)
} else {
unfixedArguments = arguments
fixedArguments = []
}

self.arguments = unfixedArguments.map { argument in
if argument.first == "-" {
let flags = argument[argument.index(after: argument.startIndex)..<argument.endIndex]

Expand All @@ -72,6 +84,7 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible

return .argument(argument)
}
self.arguments.append(contentsOf: fixedArguments.map { .argument($0) })
}

public init(parser: ArgumentParser) throws {
Expand Down
25 changes: 25 additions & 0 deletions Tests/CommanderTests/ArgumentParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,30 @@ public func testArgumentParser() {
try expect(values.count) == 0
}
}
$0.describe("positional arguments") {
$0.before {
parser = ArgumentParser(arguments: ["-a", "value1", "--", "-b", "value2"])
}
$0.it("should not count double dash into the arguments") {
try expect(parser.remainder.count) == 4
_ = try parser.shiftValue(for: "a" as ArgumentParser.Flag)
try expect(parser.remainder.count) == 2
}
$0.it("should not parse parameters as flags after the double dash") {
try expect(parser.hasFlag("a" as ArgumentParser.Flag)).to.beTrue()
try expect(parser.hasFlag("b" as ArgumentParser.Flag)).to.beFalse()

_ = try parser.shiftValue(for: "a" as ArgumentParser.Flag)
let value = try parser.shiftValue(for: "b" as ArgumentParser.Flag)
try expect(value).to.beNil()
}
$0.it("should return parameters after the double dash as arguments") {
_ = try parser.shiftValue(for: "a" as ArgumentParser.Flag)
let value1 = parser.shift()
let value2 = parser.shift()
try expect(value1) == "-b"
try expect(value2) == "value2"
}
}
}
}