0001    //
0002    //  CommandArguments.swift
0003    //  Example
0004    //
0005    //  Created by Jake Heiser on 3/7/15.
0006    //  Copyright (c) 2015 jakeheis. All rights reserved.
0007    //
0008    
0009    public class CommandArguments
CLI.swift:144
    class private func setupOptionsAndArguments(command: CommandType, arguments: RawArguments) throws -> (execute: Bool, arguments: CommandArguments?) {
CLI.swift:167
        return (true, try CommandArguments.fromRawArguments(arguments, signature: commandSignature))
Command.swift:32
    func execute(arguments: CommandArguments) throws
CommandArguments.swift:27
    class func fromRawArguments(rawArguments: RawArguments, signature: CommandSignature) throws -> CommandArguments {
CommandArguments.swift:46
        let commandArguments = CommandArguments()
CommandArguments.swift:85
    private class func handleEmptySignature(rawArguments rawArguments: RawArguments) throws -> CommandArguments {
CommandArguments.swift:90
        return CommandArguments()
HelpCommand.swift:33
    public func execute(arguments: CommandArguments) throws {
LightweightCommand.swift:21
    public typealias ExecutionBlock = (arguments: CommandArguments) throws -> ()
LightweightCommand.swift:35
    public func execute(arguments: CommandArguments) throws {
VersionCommand.swift:27
    public func execute(arguments: CommandArguments) throws  {
{ 0010 0011 var keyedArguments
CommandArguments.swift:14
        self.keyedArguments = [:]
CommandArguments.swift:18
        self.keyedArguments = keyedArguments
CommandArguments.swift:22
        self.keyedArguments = [:]
CommandArguments.swift:107
            return keyedArguments[key]
CommandArguments.swift:110
            keyedArguments[key] = newArgument
CommandArguments.swift:133
        if let arg = keyedArguments[key] as? String {
CommandArguments.swift:156
        if let arg = keyedArguments[key] as? [String] {
: [String: AnyObject] 0012 0013 init
CommandArguments.swift:46
        let commandArguments = CommandArguments()
CommandArguments.swift:90
        return CommandArguments()
() { 0014 self.keyedArguments = [:] 0015 } 0016 0017 init(keyedArguments: [String: AnyObject]) { 0018 self.keyedArguments = keyedArguments 0019 } 0020 0021 init(rawArguments: RawArguments, signature: CommandSignature) { 0022 self.keyedArguments = [:] 0023 } 0024 0025 // Keying arguments 0026 0027 class func fromRawArguments
CLI.swift:167
        return (true, try CommandArguments.fromRawArguments(arguments, signature: commandSignature))
(rawArguments: RawArguments, signature: CommandSignature) throws -> CommandArguments { 0028 if signature.isEmpty { 0029 return try handleEmptySignature(rawArguments: rawArguments) 0030 } 0031 0032 let arguments = rawArguments.unclassifiedArguments() 0033 0034 if arguments.count < signature.requiredParameters.count { 0035 throw CLIError.Error(errorMessage(expectedCount: signature.requiredParameters.count, givenCount: arguments.count)) 0036 } 0037 0038 if !signature.collectRemainingArguments && signature.optionalParameters.isEmpty && arguments.count != signature.requiredParameters.count { 0039 throw CLIError.Error(errorMessage(expectedCount: signature.requiredParameters.count, givenCount: arguments.count)) 0040 } 0041 0042 if !signature.collectRemainingArguments && arguments.count > signature.requiredParameters.count + signature.optionalParameters.count { 0043 throw CLIError.Error(errorMessage(expectedCount: signature.requiredParameters.count + signature.optionalParameters.count, givenCount: arguments.count)) 0044 } 0045 0046 let commandArguments = CommandArguments() 0047 0048 // First handle required arguments 0049 for i in 0..<signature.requiredParameters.count { 0050 let parameter = signature.requiredParameters[i] 0051 let value = arguments[i] 0052 commandArguments[parameter] = value 0053 } 0054 0055 // Then handle optional arguments if there are any 0056 if !signature.optionalParameters.isEmpty && arguments.count > signature.requiredParameters.count { 0057 for i in 0..<signature.optionalParameters.count { 0058 let index = i + signature.requiredParameters.count 0059 if index >= arguments.count { 0060 break 0061 } 0062 let parameter = signature.optionalParameters[i] 0063 let value = arguments[index] 0064 commandArguments[parameter] = value 0065 } 0066 } 0067 0068 // Finally collect the remaining arguments into an array if ... is present 0069 if signature.collectRemainingArguments { 0070 let parameter = signature.optionalParameters.isEmpty ? signature.requiredParameters[signature.requiredParameters.count-1] : signature.optionalParameters[signature.optionalParameters.count-1] 0071 0072 var collectedArgument = [commandArguments.requiredArgument(parameter)] 0073 0074 let startingIndex = signature.requiredParameters.count + signature.optionalParameters.count 0075 for i in startingIndex..<arguments.count { 0076 collectedArgument.append(arguments[i]) 0077 } 0078 0079 commandArguments[parameter] = collectedArgument 0080 } 0081 0082 return commandArguments 0083 } 0084 0085 private class func handleEmptySignature
CommandArguments.swift:29
            return try handleEmptySignature(rawArguments: rawArguments)
(rawArguments rawArguments: RawArguments) throws -> CommandArguments { 0086 guard rawArguments.unclassifiedArguments().count == 0 else { 0087 throw CLIError.Error("Expected no arguments, got \(rawArguments.unclassifiedArguments().count).") 0088 } 0089 0090 return CommandArguments() 0091 } 0092 0093 private class func errorMessage
CommandArguments.swift:35
            throw CLIError.Error(errorMessage(expectedCount: signature.requiredParameters.count, givenCount: arguments.count))
CommandArguments.swift:39
            throw CLIError.Error(errorMessage(expectedCount: signature.requiredParameters.count, givenCount: arguments.count))
CommandArguments.swift:43
            throw CLIError.Error(errorMessage(expectedCount: signature.requiredParameters.count + signature.optionalParameters.count, givenCount: arguments.count))
(expectedCount expectedCount: Int, givenCount: Int) -> String { 0094 let argString = expectedCount == 1 ? "argument" : "arguments" 0095 return "Expected \(expectedCount) \(argString), but got \(givenCount)." 0096 } 0097 0098 // MARK: - Subscripting 0099 0100 /** 0101 Generic subscripting of arguments 0102 0103 - SeeAlso: Typesafe shortcuts such as `args.requiredArguments("arg")` 0104 */ 0105 public subscript
CommandArguments.swift:52
            commandArguments[parameter] = value
CommandArguments.swift:64
                commandArguments[parameter] = value
CommandArguments.swift:79
            commandArguments[parameter] = collectedArgument
(key: String) -> AnyObject? { 0106 get { 0107 return keyedArguments[key] 0108 } 0109 set(newArgument) { 0110 keyedArguments[key] = newArgument 0111 } 0112 } 0113 0114 // MARK: - Typesafe shortcuts 0115 0116 /** 0117 Subscripting shortcut for arguments guaranteed to be present. Only use for arguments 0118 in the command signature of the form `<requiredArgument>` 0119 0120 - Parameter key: the name of the argument as seen in the command signature 0121 */ 0122 public func requiredArgument
CommandArguments.swift:72
            var collectedArgument = [commandArguments.requiredArgument(parameter)]
(key: String) -> String { 0123 return optionalArgument(key)! 0124 } 0125 0126 /** 0127 Subscripting shortcut for arguments sometimes present. Only use for arguments 0128 in the command signature of the form `[<optionalArgument>]` 0129 0130 - Parameter key: the name of the argument as seen in the command signature 0131 */ 0132 public func optionalArgument
CommandArguments.swift:123
        return optionalArgument(key)!
(key: String) -> String? { 0133 if let arg = keyedArguments[key] as? String { 0134 return arg 0135 } 0136 return nil 0137 } 0138 0139 /** 0140 Subscripting shortcut for a collected argument guaranteed to be present. Only use 0141 for arguments in the command signature of the form `<requiredArgument> ...` 0142 0143 - Parameter key: the name of the argument as seen in the command signature 0144 */ 0145 public func requiredCollectedArgument(key: String) -> [String] { 0146 return optionalCollectedArgument(key)! 0147 } 0148 0149 /** 0150 Subscripting shortcut for a collected argument sometimes present. Only use 0151 for arguments in the command signature of the form `[<optionalArgument>] ...` 0152 0153 - Parameter key: the name of the argument as seen in the command signature 0154 */ 0155 public func optionalCollectedArgument
CommandArguments.swift:146
        return optionalCollectedArgument(key)!
(key: String) -> [String]? { 0156 if let arg = keyedArguments[key] as? [String] { 0157 return arg 0158 } 0159 return nil 0160 } 0161 0162 } 0163