|
| 1 | +public enum ArgumentType { |
| 2 | + case Argument |
| 3 | + case Option |
| 4 | +} |
| 5 | + |
| 6 | +public protocol ArgumentDescriptor { |
| 7 | + typealias ValueType |
| 8 | + |
| 9 | + /// The arguments name |
| 10 | + var name:String { get } |
| 11 | + |
| 12 | + /// The arguments description |
| 13 | + var description:String? { get } |
| 14 | + |
| 15 | + var type:ArgumentType { get } |
| 16 | + |
| 17 | + /// Parse the argument |
| 18 | + func parse(parser:ArgumentParser) throws -> ValueType |
| 19 | +} |
| 20 | + |
| 21 | +extension ArgumentConvertible { |
| 22 | + init(string: String) throws { |
| 23 | + try self.init(parser: ArgumentParser(arguments: [string])) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +public class Argument<T : ArgumentConvertible> : ArgumentDescriptor { |
| 28 | + public typealias ValueType = T |
| 29 | + |
| 30 | + public let name:String |
| 31 | + public let description:String? |
| 32 | + |
| 33 | + public var type:ArgumentType { return .Argument } |
| 34 | + |
| 35 | + public init(_ name:String, description:String? = nil) { |
| 36 | + self.name = name |
| 37 | + self.description = description |
| 38 | + } |
| 39 | + |
| 40 | + public func parse(parser:ArgumentParser) throws -> ValueType { |
| 41 | + return try T(parser: parser) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +public class Option<T : ArgumentConvertible> : ArgumentDescriptor { |
| 47 | + public typealias ValueType = T |
| 48 | + |
| 49 | + public let name:String |
| 50 | + public let description:String? |
| 51 | + public let `default`:ValueType |
| 52 | + public var type:ArgumentType { return .Option } |
| 53 | + |
| 54 | + public init(_ name:String, _ `default`:ValueType, description:String? = nil) { |
| 55 | + self.name = name |
| 56 | + self.description = description |
| 57 | + self.`default` = `default` |
| 58 | + } |
| 59 | + |
| 60 | + public func parse(parser:ArgumentParser) throws -> ValueType { |
| 61 | + if let value = try parser.shiftValueForOption(name) { |
| 62 | + return try T(string: value) |
| 63 | + } |
| 64 | + |
| 65 | + return `default` |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +public class Options<T : ArgumentConvertible> : ArgumentDescriptor { |
| 70 | + public typealias ValueType = [T] |
| 71 | + |
| 72 | + public let name:String |
| 73 | + public let description:String? |
| 74 | + public let count:Int |
| 75 | + public let `default`:ValueType |
| 76 | + public var type:ArgumentType { return .Option } |
| 77 | + |
| 78 | + public init(_ name:String, _ `default`:ValueType, count: Int, description:String? = nil) { |
| 79 | + self.name = name |
| 80 | + self.`default` = `default` |
| 81 | + self.count = count |
| 82 | + self.description = description |
| 83 | + } |
| 84 | + |
| 85 | + public func parse(parser:ArgumentParser) throws -> ValueType { |
| 86 | + let values = try parser.shiftValuesForOption(name, count: count) |
| 87 | + return try values?.map { try T(string: $0) } ?? `default` |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +public class Flag : ArgumentDescriptor { |
| 92 | + public typealias ValueType = Bool |
| 93 | + |
| 94 | + public let name:String |
| 95 | + public let flag:Character? |
| 96 | + public let description:String? |
| 97 | + public let `default`:ValueType |
| 98 | + public var type:ArgumentType { return .Option } |
| 99 | + |
| 100 | + public init(_ name:String, flag:Character? = nil, description:String? = nil, `default`:Bool = false) { |
| 101 | + self.name = name |
| 102 | + self.flag = flag |
| 103 | + self.description = description |
| 104 | + self.`default` = `default` |
| 105 | + } |
| 106 | + |
| 107 | + public func parse(parser:ArgumentParser) throws -> ValueType { |
| 108 | + if parser.hasOption("no-\(name)") { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + if parser.hasOption(name) { |
| 113 | + return true |
| 114 | + } |
| 115 | + |
| 116 | + if let flag = flag { |
| 117 | + if parser.hasFlag(flag) { |
| 118 | + return true |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return `default` |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class BoxedArgumentDescriptor { |
| 127 | + let name:String |
| 128 | + let description:String? |
| 129 | + let `default`:String? |
| 130 | + let type:ArgumentType |
| 131 | + |
| 132 | + init<T : ArgumentDescriptor>(value:T) { |
| 133 | + name = value.name |
| 134 | + description = value.description |
| 135 | + type = value.type |
| 136 | + |
| 137 | + if let value = value as? Flag { |
| 138 | + `default` = value.`default`.description |
| 139 | + } else { |
| 140 | + // TODO, default for Option and Options |
| 141 | + `default` = nil |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +class Help : ErrorType, CustomStringConvertible { |
| 147 | + let command:String? |
| 148 | + let group:Group? |
| 149 | + let descriptors:[BoxedArgumentDescriptor] |
| 150 | + |
| 151 | + init(_ descriptors:[BoxedArgumentDescriptor], command:String? = nil, group:Group? = nil) { |
| 152 | + self.command = command |
| 153 | + self.group = group |
| 154 | + self.descriptors = descriptors |
| 155 | + } |
| 156 | + |
| 157 | + func reraise(command:String? = nil) -> Help { |
| 158 | + if let oldCommand = self.command, newCommand = command { |
| 159 | + return Help(descriptors, command: "\(newCommand) \(oldCommand)") |
| 160 | + } |
| 161 | + return Help(descriptors, command: command ?? self.command) |
| 162 | + } |
| 163 | + |
| 164 | + var description:String { |
| 165 | + var output = [String]() |
| 166 | + |
| 167 | + let arguments = descriptors.filter { $0.type == ArgumentType.Argument } |
| 168 | + let options = descriptors.filter { $0.type == ArgumentType.Option } |
| 169 | + |
| 170 | + if let command = command { |
| 171 | + let args = arguments.map { $0.name } |
| 172 | + let usage = ([command] + args).joinWithSeparator(" ") |
| 173 | + |
| 174 | + output.append("Usage:") |
| 175 | + output.append("") |
| 176 | + output.append(" \(usage)") |
| 177 | + output.append("") |
| 178 | + } |
| 179 | + |
| 180 | + if let group = group { |
| 181 | + output.append("Commands:") |
| 182 | + output.append("") |
| 183 | + for (name, _) in group.commands { |
| 184 | + output.append(" + \(name)") |
| 185 | + } |
| 186 | + output.append("") |
| 187 | + } |
| 188 | + |
| 189 | + if !options.isEmpty { |
| 190 | + output.append("Options:") |
| 191 | + for option in options { |
| 192 | + // TODO: default, [default: `\(`default`)`] |
| 193 | + |
| 194 | + if let description = option.description { |
| 195 | + output.append(" --\(option.name) - \(description)") |
| 196 | + } else { |
| 197 | + output.append(" --\(option.name)") |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return output.joinWithSeparator("\n") |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +public func command<A : ArgumentDescriptor>(descriptor:A, closure:((A.ValueType) -> ())) -> CommandType { |
| 208 | + return AnonymousCommand { parser in |
| 209 | + if parser.hasOption("help") { |
| 210 | + throw Help([BoxedArgumentDescriptor(value: descriptor)]) |
| 211 | + } |
| 212 | + |
| 213 | + closure(try descriptor.parse(parser)) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +public func command<A:ArgumentDescriptor, B:ArgumentDescriptor>(descriptorA:A, _ descriptorB:B, closure:((A.ValueType, B.ValueType) -> ())) -> CommandType { |
| 218 | + return AnonymousCommand { parser in |
| 219 | + if parser.hasOption("help") { |
| 220 | + throw Help([ |
| 221 | + BoxedArgumentDescriptor(value: descriptorA), |
| 222 | + BoxedArgumentDescriptor(value: descriptorB), |
| 223 | + ]) |
| 224 | + } |
| 225 | + |
| 226 | + closure(try descriptorA.parse(parser), try descriptorB.parse(parser)) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +public func command<A:ArgumentDescriptor, B:ArgumentDescriptor, C:ArgumentDescriptor>(descriptorA:A, _ descriptorB:B, _ descriptorC:C, closure:((A.ValueType, B.ValueType, C.ValueType) -> ())) -> CommandType { |
| 231 | + return AnonymousCommand { parser in |
| 232 | + if parser.hasOption("help") { |
| 233 | + throw Help([ |
| 234 | + BoxedArgumentDescriptor(value: descriptorA), |
| 235 | + BoxedArgumentDescriptor(value: descriptorB), |
| 236 | + BoxedArgumentDescriptor(value: descriptorC), |
| 237 | + ]) |
| 238 | + } |
| 239 | + |
| 240 | + closure(try descriptorA.parse(parser), try descriptorB.parse(parser), try descriptorC.parse(parser)) |
| 241 | + } |
| 242 | +} |
0 commit comments