0001    //
0002    //  Errors.swift
0003    //  Commandant
0004    //
0005    //  Created by Justin Spahr-Summers on 2014-10-24.
0006    //  Copyright (c) 2014 Carthage. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    import Result
0011    
0012    /// Possible errors that can originate from Commandant.
0013    ///
0014    /// `ClientError` should be the type of error (if any) that can occur when
0015    /// running commands.
0016    public enum CommandantError
Argument.swift:28
	private func invalidUsageError<ClientError>(value: String) -> CommandantError<ClientError> {
Command.swift:39
	public let usage: () -> CommandantError<ClientError>?
Command.swift:62
		usage = { () -> CommandantError<ClientError>? in
Errors.swift:24
extension CommandantError: CustomStringConvertible {
Errors.swift:38
internal func missingArgumentError<ClientError>(argumentName: String) -> CommandantError<ClientError> {
Errors.swift:45
internal func informativeUsageError<ClientError>(keyValueExample: String, usage: String) -> CommandantError<ClientError> {
Errors.swift:55
internal func combineUsageErrors<ClientError>(lhs: CommandantError<ClientError>, _ rhs: CommandantError<ClientError>) -> CommandantError<ClientError> {
Errors.swift:55
internal func combineUsageErrors<ClientError>(lhs: CommandantError<ClientError>, _ rhs: CommandantError<ClientError>) -> CommandantError<ClientError> {
Errors.swift:55
internal func combineUsageErrors<ClientError>(lhs: CommandantError<ClientError>, _ rhs: CommandantError<ClientError>) -> CommandantError<ClientError> {
Errors.swift:70
internal func unrecognizedArgumentsError<ClientError>(options: [String]) -> CommandantError<ClientError> {
Errors.swift:78
internal func informativeUsageError<T, ClientError>(valueExample: String, argument: Argument<T>) -> CommandantError<ClientError> {
Errors.swift:87
internal func informativeUsageError<T: ArgumentType, ClientError>(argument: Argument<T>) -> CommandantError<ClientError> {
Errors.swift:105
internal func informativeUsageError<T: ArgumentType, ClientError>(argument: Argument<[T]>) -> CommandantError<ClientError> {
Errors.swift:126
internal func informativeUsageError<T, ClientError>(keyValueExample: String, option: Option<T>) -> CommandantError<ClientError> {
Errors.swift:135
internal func informativeUsageError<T: ArgumentType, ClientError>(option: Option<T>) -> CommandantError<ClientError> {
Errors.swift:153
internal func informativeUsageError<ClientError>(option: Option<Bool>) -> CommandantError<ClientError> {
Option.swift:83
	private func invalidUsageError<ClientError>(value: String) -> CommandantError<ClientError> {
<ClientError
Errors.swift:21
	case CommandError(ClientError)
>: ErrorType { 0017 /// An option was used incorrectly. 0018 case UsageError
Argument.swift:30
		return .UsageError(description: description)
Errors.swift:27
		case let .UsageError(description):
Errors.swift:40
	return .UsageError(description: description)
Errors.swift:48
	return .UsageError(description: lines.reduce(keyValueExample) { previous, value in
Errors.swift:57
	case let (.UsageError(left), .UsageError(right)):
Errors.swift:57
	case let (.UsageError(left), .UsageError(right)):
Errors.swift:59
		return .UsageError(description: combinedDescription)
Errors.swift:61
	case (.UsageError, _):
Errors.swift:64
	case (_, .UsageError), (_, _):
Errors.swift:71
	return .UsageError(description: "Unrecognized arguments: " + options.joinWithSeparator(", "))
Option.swift:85
		return .UsageError(description: description)
(description: String) 0019 0020 /// An error occurred while running a command. 0021 case CommandError
Errors.swift:30
		case let .CommandError(error):
(ClientError) 0022 } 0023 0024 extension CommandantError: CustomStringConvertible { 0025 public var description: String { 0026 switch self { 0027 case let .UsageError(description): 0028 return description 0029 0030 case let .CommandError(error): 0031 return String(error) 0032 } 0033 } 0034 } 0035 0036 /// Constructs an `InvalidArgument` error that indicates a missing value for 0037 /// the argument by the given name. 0038 internal func missingArgumentError<ClientError>(argumentName: String) -> CommandantError<ClientError> { 0039 let description = "Missing argument for \(argumentName)" 0040 return .UsageError(description: description) 0041 } 0042 0043 /// Constructs an error by combining the example of key (and value, if applicable) 0044 /// with the usage description. 0045 internal func informativeUsageError
Errors.swift:80
		return informativeUsageError("[\(valueExample)]", usage: argument.usage)
Errors.swift:82
		return informativeUsageError(valueExample, usage: argument.usage)
Errors.swift:128
		return informativeUsageError("[\(keyValueExample)]", usage: option.usage)
Errors.swift:130
		return informativeUsageError(keyValueExample, usage: option.usage)
<ClientError>(keyValueExample: String, usage: String) -> CommandantError<ClientError> { 0046 let lines = usage.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) 0047 0048 return .UsageError(description: lines.reduce(keyValueExample) { previous, value in 0049 return previous + "\n\t" + value 0050 }) 0051 } 0052 0053 /// Combines the text of the two errors, if they're both `UsageError`s. 0054 /// Otherwise, uses whichever one is not (biased toward the left). 0055 internal func combineUsageErrors<ClientError>(lhs: CommandantError<ClientError>, _ rhs: CommandantError<ClientError>) -> CommandantError<ClientError> { 0056 switch (lhs, rhs) { 0057 case let (.UsageError(left), .UsageError(right)): 0058 let combinedDescription = "\(left)\n\n\(right)" 0059 return .UsageError(description: combinedDescription) 0060 0061 case (.UsageError, _): 0062 return rhs 0063 0064 case (_, .UsageError), (_, _): 0065 return lhs 0066 } 0067 } 0068 0069 /// Constructs an error that indicates unrecognized arguments remains. 0070 internal func unrecognizedArgumentsError<ClientError>(options: [String]) -> CommandantError<ClientError> { 0071 return .UsageError(description: "Unrecognized arguments: " + options.joinWithSeparator(", ")) 0072 } 0073 0074 // MARK: Argument 0075 0076 /// Constructs an error that describes how to use the argument, with the given 0077 /// example of value usage if applicable. 0078 internal func informativeUsageError
Errors.swift:101
	return informativeUsageError(example, argument: argument)
Errors.swift:119
	return informativeUsageError(example, argument: argument)
<T, ClientError>(valueExample: String, argument: Argument<T>) -> CommandantError<ClientError> { 0079 if argument.defaultValue != nil { 0080 return informativeUsageError("[\(valueExample)]", usage: argument.usage) 0081 } else { 0082 return informativeUsageError(valueExample, usage: argument.usage) 0083 } 0084 } 0085 0086 /// Constructs an error that describes how to use the argument. 0087 internal func informativeUsageError<T: ArgumentType, ClientError>(argument: Argument<T>) -> CommandantError<ClientError> { 0088 var example = "" 0089 0090 var valueExample = "" 0091 if let defaultValue = argument.defaultValue { 0092 valueExample = "\(defaultValue)" 0093 } 0094 0095 if valueExample.isEmpty { 0096 example += "(\(T.name))" 0097 } else { 0098 example += valueExample 0099 } 0100 0101 return informativeUsageError(example, argument: argument) 0102 } 0103 0104 /// Constructs an error that describes how to use the argument list. 0105 internal func informativeUsageError<T: ArgumentType, ClientError>(argument: Argument<[T]>) -> CommandantError<ClientError> { 0106 var example = "" 0107 0108 var valueExample = "" 0109 if let defaultValue = argument.defaultValue { 0110 valueExample = "\(defaultValue)" 0111 } 0112 0113 if valueExample.isEmpty { 0114 example += "(\(T.name))" 0115 } else { 0116 example += valueExample 0117 } 0118 0119 return informativeUsageError(example, argument: argument) 0120 } 0121 0122 // MARK: Option 0123 0124 /// Constructs an error that describes how to use the option, with the given 0125 /// example of key (and value, if applicable) usage. 0126 internal func informativeUsageError
Errors.swift:149
	return informativeUsageError(example, option: option)
Errors.swift:157
		return informativeUsageError((defaultValue ? "--no-\(key)" : "--\(key)"), option: option)
Errors.swift:159
		return informativeUsageError("--(no-)\(key)", option: option)
<T, ClientError>(keyValueExample: String, option: Option<T>) -> CommandantError<ClientError> { 0127 if option.defaultValue != nil { 0128 return informativeUsageError("[\(keyValueExample)]", usage: option.usage) 0129 } else { 0130 return informativeUsageError(keyValueExample, usage: option.usage) 0131 } 0132 } 0133 0134 /// Constructs an error that describes how to use the option. 0135 internal func informativeUsageError<T: ArgumentType, ClientError>(option: Option<T>) -> CommandantError<ClientError> { 0136 var example = "--\(option.key) " 0137 0138 var valueExample = "" 0139 if let defaultValue = option.defaultValue { 0140 valueExample = "\(defaultValue)" 0141 } 0142 0143 if valueExample.isEmpty { 0144 example += "(\(T.name))" 0145 } else { 0146 example += valueExample 0147 } 0148 0149 return informativeUsageError(example, option: option) 0150 } 0151 0152 /// Constructs an error that describes how to use the given boolean option. 0153 internal func informativeUsageError<ClientError>(option: Option<Bool>) -> CommandantError<ClientError> { 0154 let key = option.key 0155 0156 if let defaultValue = option.defaultValue { 0157 return informativeUsageError((defaultValue ? "--no-\(key)" : "--\(key)"), option: option) 0158 } else { 0159 return informativeUsageError("--(no-)\(key)", option: option) 0160 } 0161 } 0162