Skip to content

Commit

Permalink
Merge pull request #96 from NatanRolnik/fix-switch-keys
Browse files Browse the repository at this point in the history
Fix switch keys being ignored when a Switch contains a flag
  • Loading branch information
mdiep authored Feb 3, 2017
2 parents 0f2b3f5 + c138e01 commit 58bcfb0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Sources/Commandant/Switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public func <| <ClientError> (mode: CommandMode, option: Switch) -> Result<Bool,
switch mode {
case let .arguments(arguments):
var enabled = arguments.consume(key: option.key)
if let flag = option.flag {

if let flag = option.flag, !enabled {
enabled = arguments.consumeBoolean(flag: flag)
}
return .success(enabled)
Expand Down
20 changes: 19 additions & 1 deletion Tests/CommandantTests/OptionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,30 @@ class OptionsProtocolSpec: QuickSpec {
expect(value).to(equal(expected))
}

it("should enable multiple boolean flags") {
it("should enable a boolean flag if a single Switch flag is passed") {
let value = tryArguments("required", "-f").value
let expected = TestOptions(intValue: 42, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: nil, optionalFilename: "filename", requiredName: "required", enabled: false, force: true, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should enable multiple boolean flags if multiple grouped Switch flags are passed") {
let value = tryArguments("required", "-fg").value
let expected = TestOptions(intValue: 42, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: nil, optionalFilename: "filename", requiredName: "required", enabled: false, force: true, glob: true, arguments: [])
expect(value).to(equal(expected))
}

it("should enable a boolean flag if a single Switch key is passed") {
let value = tryArguments("required", "--force").value
let expected = TestOptions(intValue: 42, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: nil, optionalFilename: "filename", requiredName: "required", enabled: false, force: true, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should enable multiple boolean flags if multiple Switch keys are passed") {
let value = tryArguments("required", "--force", "--glob").value
let expected = TestOptions(intValue: 42, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: nil, optionalFilename: "filename", requiredName: "required", enabled: false, force: true, glob: true, arguments: [])
expect(value).to(equal(expected))
}

it("should consume the rest of positional arguments") {
let value = tryArguments("required", "optional", "value1", "value2").value
let expected = TestOptions(intValue: 42, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: nil, optionalFilename: "optional", requiredName: "required", enabled: false, force: false, glob: false, arguments: [ "value1", "value2" ])
Expand Down

0 comments on commit 58bcfb0

Please # to comment.