Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix switch keys being ignored when a Switch contains a flag #96

Merged
merged 1 commit into from
Feb 3, 2017
Merged
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
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