Skip to content

Commit

Permalink
feat: support using = with options
Browse files Browse the repository at this point in the history
Re #89
  • Loading branch information
kylef committed Mar 26, 2022
1 parent e1ba47e commit f22f0a0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

### Enhancements

- Supporting using `=` syntax with flags. For example `-o=file.tar.gz`. This
allows specifying negative, or empty values.
- Supporting using `=` syntax with options and flags. For example
`--output=file.tar.gz` / `-o=file.tar.gz`. This allows specifying negative,
or empty values.

## 0.9.2 (2021-06-08)

Expand Down
7 changes: 7 additions & 0 deletions Sources/Commander/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible

if flags.first == "-" {
let option = flags[flags.index(after: flags.startIndex)..<flags.endIndex]

if let equalsIndex = option.range(of: "=") {
let key = option[..<equalsIndex.lowerBound]
let value = option[equalsIndex.upperBound...]
return arguments + [.option(String(key)), .argument(String(value))]
}

return arguments + [.option(String(option))]
}

Expand Down
12 changes: 12 additions & 0 deletions Tests/CommanderTests/ArgumentParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ let testArgumentParser: ((ContextType) -> Void) = {
try expect(value?.first) == "middle"
try expect(value?.last) == "end"
}

$0.it("should return equals values") {
let parser = ArgumentParser(arguments: ["--output=file.tar.gz"])
let value = try parser.shiftValue(for: "output")
try expect(value) == "file.tar.gz"
}

$0.it("should return empty values") {
let parser = ArgumentParser(arguments: ["--output="])
let value = try parser.shiftValue(for: "output")
try expect(value) == ""
}
}

$0.describe("variadic options") {
Expand Down

0 comments on commit f22f0a0

Please sign in to comment.