Skip to content

Commit

Permalink
Merge pull request #25 from Carthage/use-result
Browse files Browse the repository at this point in the history
Use antitypical/Result
  • Loading branch information
robrix committed May 5, 2015
2 parents 3866cad + b6d85f6 commit 63eabdc
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 57 deletions.
9 changes: 6 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "Carthage.checkout/LlamaKit"]
path = Carthage/Checkouts/LlamaKit
url = https://github.com/LlamaKit/LlamaKit.git
[submodule "Carthage.checkout/Nimble"]
path = Carthage/Checkouts/Nimble
url = https://github.com/Quick/Nimble.git
Expand All @@ -10,3 +7,9 @@
[submodule "Carthage.checkout/xcconfigs"]
path = Carthage/Checkouts/xcconfigs
url = https://github.com/jspahrsummers/xcconfigs.git
[submodule "Carthage/Checkouts/Box"]
path = Carthage/Checkouts/Box
url = https://github.com/robrix/Box.git
[submodule "Carthage/Checkouts/Result"]
path = Carthage/Checkouts/Result
url = https://github.com/antitypical/Result.git
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "LlamaKit/LlamaKit" == 0.6
github "antitypical/Result" ~> 0.4
6 changes: 3 additions & 3 deletions Cartfile.private
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "Quick/Quick" "master"
github "Quick/Nimble" "master"
github "jspahrsummers/xcconfigs" >= 0.6
github "Quick/Quick" ~> 0.3
github "Quick/Nimble" ~> 0.4
github "jspahrsummers/xcconfigs" ~> 0.8
9 changes: 5 additions & 4 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github "LlamaKit/LlamaKit" "v0.6.0"
github "Quick/Nimble" "6a679cbf20da3fe570c26296b3029dd168b72c00"
github "Quick/Quick" "155f858bce082373fc60d0c0a63415bf336c676b"
github "jspahrsummers/xcconfigs" "0.7.2"
github "robrix/Box" "1.2.1"
github "Quick/Nimble" "v0.4.2"
github "Quick/Quick" "v0.3.1"
github "jspahrsummers/xcconfigs" "0.8"
github "antitypical/Result" "0.4"
1 change: 0 additions & 1 deletion Carthage/Checkouts/LlamaKit
Submodule LlamaKit deleted from e28d7f
2 changes: 1 addition & 1 deletion Carthage/Checkouts/Nimble
2 changes: 1 addition & 1 deletion Carthage/Checkouts/xcconfigs
14 changes: 8 additions & 6 deletions Commandant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
/* Begin PBXBuildFile section */
D00CCDDF1A20717400109F8C /* Commandant.h in Headers */ = {isa = PBXBuildFile; fileRef = D00CCDDE1A20717400109F8C /* Commandant.h */; settings = {ATTRIBUTES = (Public, ); }; };
D00CCDE51A20717400109F8C /* Commandant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCDD91A20717400109F8C /* Commandant.framework */; };
D00CCE201A2073CE00109F8C /* LlamaKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCE1F1A2073CE00109F8C /* LlamaKit.framework */; };
D00CCE211A2073D200109F8C /* LlamaKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCE1F1A2073CE00109F8C /* LlamaKit.framework */; };
D00CCE241A2073DA00109F8C /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCE221A2073DA00109F8C /* Nimble.framework */; };
D00CCE251A2073DA00109F8C /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCE231A2073DA00109F8C /* Quick.framework */; };
D00CCE271A20741300109F8C /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE261A20741300109F8C /* Command.swift */; };
D00CCE291A20741C00109F8C /* OptionSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE281A20741C00109F8C /* OptionSpec.swift */; };
D00CCE2B1A20748500109F8C /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE2A1A20748500109F8C /* Errors.swift */; };
D00CCE2D1A2075ED00109F8C /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE2C1A2075ED00109F8C /* ArgumentParser.swift */; };
D00CCE2F1A2075F700109F8C /* Option.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE2E1A2075F700109F8C /* Option.swift */; };
D02130991AF87D6E00B9EC20 /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D02130971AF87D6E00B9EC20 /* Box.framework */; };
D021309A1AF87D6E00B9EC20 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D02130981AF87D6E00B9EC20 /* Result.framework */; };
D0BF14FB1A4C8957003147BC /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0BF14FA1A4C8957003147BC /* HelpCommand.swift */; };
D8169D871ACB942D00923FB0 /* Switch.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8169D861ACB942D00923FB0 /* Switch.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -56,14 +56,15 @@
D00CCE0B1A20719500109F8C /* Mac-Framework.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Mac-Framework.xcconfig"; sourceTree = "<group>"; };
D00CCE0C1A20719500109F8C /* Mac-StaticLibrary.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Mac-StaticLibrary.xcconfig"; sourceTree = "<group>"; };
D00CCE0D1A20719500109F8C /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
D00CCE1F1A2073CE00109F8C /* LlamaKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = LlamaKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D00CCE221A2073DA00109F8C /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D00CCE231A2073DA00109F8C /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Quick.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D00CCE261A20741300109F8C /* Command.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Command.swift; sourceTree = "<group>"; };
D00CCE281A20741C00109F8C /* OptionSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptionSpec.swift; sourceTree = "<group>"; };
D00CCE2A1A20748500109F8C /* Errors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = "<group>"; };
D00CCE2C1A2075ED00109F8C /* ArgumentParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgumentParser.swift; sourceTree = "<group>"; };
D00CCE2E1A2075F700109F8C /* Option.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Option.swift; sourceTree = "<group>"; };
D02130971AF87D6E00B9EC20 /* Box.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Box.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D02130981AF87D6E00B9EC20 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D0BF14FA1A4C8957003147BC /* HelpCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HelpCommand.swift; sourceTree = "<group>"; };
D8169D861ACB942D00923FB0 /* Switch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Switch.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand All @@ -73,7 +74,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D00CCE201A2073CE00109F8C /* LlamaKit.framework in Frameworks */,
D02130991AF87D6E00B9EC20 /* Box.framework in Frameworks */,
D021309A1AF87D6E00B9EC20 /* Result.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -84,7 +86,6 @@
D00CCE251A2073DA00109F8C /* Quick.framework in Frameworks */,
D00CCE241A2073DA00109F8C /* Nimble.framework in Frameworks */,
D00CCDE51A20717400109F8C /* Commandant.framework in Frameworks */,
D00CCE211A2073D200109F8C /* LlamaKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -125,7 +126,8 @@
D00CCDDC1A20717400109F8C /* Supporting Files */ = {
isa = PBXGroup;
children = (
D00CCE1F1A2073CE00109F8C /* LlamaKit.framework */,
D02130971AF87D6E00B9EC20 /* Box.framework */,
D02130981AF87D6E00B9EC20 /* Result.framework */,
D00CCDDD1A20717400109F8C /* Info.plist */,
);
name = "Supporting Files";
Expand Down
9 changes: 6 additions & 3 deletions Commandant.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Commandant/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import LlamaKit
import Result

/// Represents an argument passed on the command line.
private enum RawArgument: Equatable {
Expand Down Expand Up @@ -131,13 +131,13 @@ public final class ArgumentParser {
}
}

return failure(missingArgumentError("--\(key)"))
return .failure(missingArgumentError("--\(key)"))
} else {
rawArguments.append(arg)
}
}

return success(foundValue)
return .success(foundValue)
}

/// Returns the next positional argument that hasn't yet been returned, or
Expand Down
6 changes: 3 additions & 3 deletions Commandant/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import LlamaKit
import Result

/// Represents a subcommand that can be executed with its own set of arguments.
public protocol CommandType {
Expand Down Expand Up @@ -120,12 +120,12 @@ extension CommandRegistry {
exit(EXIT_SUCCESS)

case let .Some(.Failure(error)):
switch error.unbox {
switch error.value {
case let .UsageError(description):
fputs(description + "\n", stderr)

case let .CommandError(error):
errorHandler(error.unbox)
errorHandler(error.value)
}

exit(EXIT_FAILURE)
Expand Down
3 changes: 2 additions & 1 deletion Commandant/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//

import Foundation
import LlamaKit
import Box
import Result

/// Possible errors that can originate from Commandant.
///
Expand Down
4 changes: 2 additions & 2 deletions Commandant/HelpCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import LlamaKit
import Result

/// A basic implementation of a `help` command, using information available in a
/// `CommandRegistry`.
Expand Down Expand Up @@ -56,7 +56,7 @@ public struct HelpCommand<ClientError>: CommandType {
println(" \(formattedVerb) \(command.function)")
}

return success(())
return .success(())
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions Commandant/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import LlamaKit
import Result

/// Represents a record of options for a command, which can be parsed from
/// a list of command-line arguments.
Expand Down Expand Up @@ -167,17 +167,17 @@ public func <*> <T, U, ClientError>(f: T -> U, value: Result<T, CommandantError<
public func <*> <T, U, ClientError>(f: Result<(T -> U), CommandantError<ClientError>>, value: Result<T, CommandantError<ClientError>>) -> Result<U, CommandantError<ClientError>> {
switch (f, value) {
case let (.Failure(left), .Failure(right)):
return failure(combineUsageErrors(left.unbox, right.unbox))
return .failure(combineUsageErrors(left.value, right.value))

case let (.Failure(left), .Success):
return failure(left.unbox)
return .failure(left.value)

case let (.Success, .Failure(right)):
return failure(right.unbox)
return .failure(right.value)

case let (.Success(f), .Success(value)):
let newValue = f.unbox(value.unbox)
return success(newValue)
let newValue = f.value(value.value)
return .success(newValue)
}
}

Expand All @@ -192,12 +192,12 @@ public func <| <T: ArgumentType, ClientError>(mode: CommandMode, option: Option<
if let key = option.key {
switch arguments.consumeValueForKey(key) {
case let .Success(value):
stringValue = value.unbox
stringValue = value.value

case let .Failure(error):
switch error.unbox {
switch error.value {
case let .UsageError(description):
return failure(.UsageError(description: description))
return .failure(.UsageError(description: description))

case .CommandError:
fatalError("CommandError should be impossible when parameterized over NoError")
Expand All @@ -209,18 +209,18 @@ public func <| <T: ArgumentType, ClientError>(mode: CommandMode, option: Option<

if let stringValue = stringValue {
if let value = T.fromString(stringValue) {
return success(value)
return .success(value)
}

return failure(option.invalidUsageError(stringValue))
return .failure(option.invalidUsageError(stringValue))
} else if let defaultValue = option.defaultValue {
return success(defaultValue)
return .success(defaultValue)
} else {
return failure(missingArgumentError(option.description))
return .failure(missingArgumentError(option.description))
}

case .Usage:
return failure(informativeUsageError(option))
return .failure(informativeUsageError(option))
}
}

Expand All @@ -234,14 +234,14 @@ public func <| <ClientError>(mode: CommandMode, option: Option<Bool>) -> Result<
switch mode {
case let .Arguments(arguments):
if let value = arguments.consumeBooleanKey(option.key!) {
return success(value)
return .success(value)
} else if let defaultValue = option.defaultValue {
return success(defaultValue)
return .success(defaultValue)
} else {
return failure(missingArgumentError(option.description))
return .failure(missingArgumentError(option.description))
}

case .Usage:
return failure(informativeUsageError(option))
return .failure(informativeUsageError(option))
}
}
6 changes: 3 additions & 3 deletions Commandant/Switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright (c) 2015 Carthage. All rights reserved.
//

import LlamaKit
import Result

/// Describes a parameterless command line flag that defaults to false and can only
/// be switched on. Canonical examples include `--force` and `--recurse`.
Expand Down Expand Up @@ -56,9 +56,9 @@ public func <| <ClientError> (mode: CommandMode, option: Switch) -> Result<Bool,
if let flag = option.flag {
enabled = arguments.consumeBooleanFlag(flag)
}
return success(enabled)
return .success(enabled)

case .Usage:
return failure(informativeUsageError(option.description, option.usage))
return .failure(informativeUsageError(option.description, option.usage))
}
}
6 changes: 3 additions & 3 deletions CommandantTests/OptionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import Commandant
import Foundation
import LlamaKit
import Nimble
import Quick
import Result

enum NoError {}

Expand All @@ -22,11 +22,11 @@ class OptionsTypeSpec: QuickSpec {
}

it("should fail if a required argument is missing") {
expect(tryArguments().isSuccess).to(beFalsy())
expect(tryArguments().value).to(beNil())
}

it("should fail if an option is missing a value") {
expect(tryArguments("required", "--intValue").isSuccess).to(beFalsy())
expect(tryArguments("required", "--intValue").value).to(beNil())
}

it("should succeed without optional arguments") {
Expand Down

0 comments on commit 63eabdc

Please sign in to comment.