1+ /// Base command
12public struct Command : ExecutableCommand {
23 public let name : String
34 public let group : String ?
@@ -23,6 +24,10 @@ public struct Command: ExecutableCommand {
2324 self . closure = closure
2425 }
2526
27+ /// Create a new command.
28+ ///
29+ /// - parameters:
30+ /// - name: Name and trigger of the command.
2631 public init ( _ name: String ) {
2732 self . name = name
2833 self . group = nil
@@ -34,10 +39,18 @@ public struct Command: ExecutableCommand {
3439
3540 public func validate( ) throws { }
3641
37- public func arg< A> ( _ t: A . Type , named n: String ) -> OneArgCommand < A > {
38- let x = GenericCommandArgument < A > ( componentType: A . typedName, componentName: n)
42+ /// Add an argument to this command
43+ ///
44+ /// Command("echo")
45+ /// .arg(String.Consuming.self, named: "content")
46+ ///
47+ /// - parameters:
48+ /// - t: Type of the argument.
49+ /// - name: Name of the argument.
50+ public func arg< A> ( _ t: A . Type , named name: String ) -> OneArgCommand < A > {
51+ let x = GenericCommandArgument < A > ( componentType: A . typedName, componentName: name)
3952 return OneArgCommand < A > (
40- name: name,
53+ name: self . name,
4154 group: group,
4255 alias: alias,
4356 hookWhitelist: hookWhitelist,
@@ -72,6 +85,7 @@ fileprivate extension ExecutableCommand {
7285 }
7386}
7487
88+ /// Base command with one argument
7589public struct OneArgCommand < A> : ExecutableCommand where A: CommandArgumentConvertible {
7690 public let name : String
7791 public let group : String ?
@@ -107,20 +121,29 @@ public struct OneArgCommand<A>: ExecutableCommand where A: CommandArgumentConver
107121 try closure ( hooks, event, a)
108122 }
109123
110- public func arg< B> ( _ t: B . Type , named: String ) -> TwoArgCommand < A , B > {
124+ /// Add an argument to this command
125+ ///
126+ /// Command("echo")
127+ /// .arg(String.Consuming.self, named: "content")
128+ ///
129+ /// - parameters:
130+ /// - t: Type of the argument.
131+ /// - name: Name of the argument.
132+ public func arg< B> ( _ t: B . Type , named name: String ) -> TwoArgCommand < A , B > {
111133 return TwoArgCommand < A , B > (
112- name: name,
134+ name: self . name,
113135 group: group,
114136 alias: alias,
115137 hookWhitelist: hookWhitelist,
116138 permissionChecks: permissionChecks,
117139 closure: { _, _, _, _ in } ,
118140 argOne: arg,
119- argTwo: GenericCommandArgument < B > ( componentType: B . typedName, componentName: named )
141+ argTwo: GenericCommandArgument < B > ( componentType: B . typedName, componentName: name )
120142 )
121143 }
122144}
123145
146+ /// Base command with two arguments
124147public struct TwoArgCommand < A, B> : ExecutableCommand where A: CommandArgumentConvertible , B: CommandArgumentConvertible {
125148 public let name : String
126149 public let group : String ?
@@ -163,21 +186,30 @@ public struct TwoArgCommand<A, B>: ExecutableCommand where A: CommandArgumentCon
163186 try self . closure ( hooks, event, a, b)
164187 }
165188
166- public func arg< C> ( _ t: C . Type , named: String ) -> ThreeArgCommand < A , B , C > {
189+ /// Add an argument to this command
190+ ///
191+ /// Command("echo")
192+ /// .arg(String.Consuming.self, named: "content")
193+ ///
194+ /// - parameters:
195+ /// - t: Type of the argument.
196+ /// - name: Name of the argument.
197+ public func arg< C> ( _ t: C . Type , named name: String ) -> ThreeArgCommand < A , B , C > {
167198 return ThreeArgCommand < A , B , C > (
168- name: name,
199+ name: self . name,
169200 group: group,
170201 alias: alias,
171202 hookWhitelist: hookWhitelist,
172203 permissionChecks: permissionChecks,
173204 closure: { _, _, _, _, _ in } ,
174205 argOne: argOne,
175206 argTwo: argTwo,
176- argThree: GenericCommandArgument < C > ( componentType: C . typedName, componentName: named )
207+ argThree: GenericCommandArgument < C > ( componentType: C . typedName, componentName: name )
177208 )
178209 }
179210}
180211
212+ /// Base command with three arguments
181213public struct ThreeArgCommand < A, B, C> : ExecutableCommand where A: CommandArgumentConvertible , B: CommandArgumentConvertible , C: CommandArgumentConvertible {
182214 public let name : String
183215 public let group : String ?
@@ -226,26 +258,36 @@ public struct ThreeArgCommand<A, B, C>: ExecutableCommand where A: CommandArgume
226258 try self . closure ( hooks, event, a, b, c)
227259 }
228260
229- public func arg< T> ( _ t: T . Type , named: String ) -> ArrayArgCommand where T: CommandArgumentConvertible {
261+ /// Add an argument to this command
262+ ///
263+ /// Command("echo")
264+ /// .arg(String.Consuming.self, named: "content")
265+ ///
266+ /// - parameters:
267+ /// - t: Type of the argument.
268+ /// - name: Name of the argument.
269+ public func arg< T> ( _ t: T . Type , named name: String ) -> ArrayArgCommand where T: CommandArgumentConvertible {
230270 return ArrayArgCommand (
231- name: name,
271+ name: self . name,
232272 group: group,
233273 alias: alias,
234274 hookWhitelist: hookWhitelist,
235275 permissionChecks: permissionChecks,
236276 closure: { _, _, _ in } ,
237- arguments: [ argOne, argTwo, argThree, GenericCommandArgument < T > ( componentType: T . typedName, componentName: named ) ]
277+ arguments: [ argOne, argTwo, argThree, GenericCommandArgument < T > ( componentType: T . typedName, componentName: name ) ]
238278 )
239279 }
240280}
241281
282+ /// Base command with four or more arguments
242283public struct ArrayArgCommand : ExecutableCommand {
243284 public let name : String
244285 public let group : String ?
245286 public let alias : [ String ]
246287 public let hookWhitelist : [ HookID ]
247288 public let permissionChecks : [ CommandPermissionChecker ]
248289 public let closure : ( SwiftHooks , CommandEvent , Arguments ) throws -> Void
290+ /// Arguments for this command.
249291 public let arguments : [ CommandArgument ]
250292
251293 public var readableArguments : String ? {
@@ -278,19 +320,29 @@ public struct ArrayArgCommand: ExecutableCommand {
278320 try closure ( hooks, event, Arguments ( arguments) )
279321 }
280322
281- public func arg< T> ( _ t: T . Type , named: String ) -> ArrayArgCommand where T: CommandArgumentConvertible {
323+ /// Add an argument to this command
324+ ///
325+ /// Command("echo")
326+ /// .arg(String.Consuming.self, named: "content")
327+ ///
328+ /// - parameters:
329+ /// - t: Type of the argument.
330+ /// - name: Name of the argument.
331+ public func arg< T> ( _ t: T . Type , named name: String ) -> ArrayArgCommand where T: CommandArgumentConvertible {
282332 return ArrayArgCommand (
283- name: name,
333+ name: self . name,
284334 group: group,
285335 alias: alias,
286336 hookWhitelist: hookWhitelist,
287337 permissionChecks: permissionChecks,
288338 closure: { _, _, _ in } ,
289- arguments: arguments + GenericCommandArgument < T > ( componentType: T . typedName, componentName: named )
339+ arguments: arguments + GenericCommandArgument < T > ( componentType: T . typedName, componentName: name )
290340 )
291341 }
292342}
293343
344+
345+ /// Arguments container used in `ArrayArgCommand`.
294346public class Arguments {
295347 let arguments : [ CommandArgument ]
296348 private( set) var nilArgs : [ String ]
@@ -300,10 +352,20 @@ public class Arguments {
300352 self . nilArgs = [ ]
301353 }
302354
303- public func getArg< A> ( named name: String , on event: CommandEvent ) throws -> A where A: CommandArgumentConvertible , A. ResolvedArgument == A {
304- return try self . get ( A . self, named: name, on: event)
305- }
306-
355+ /// Resolve an argument from the command arguments
356+ ///
357+ /// let reason = try args.get(String.self, named: "reason", on: event)
358+ ///
359+ /// - parameters:
360+ /// - arg: Type to resolve.
361+ /// - name: Name of the argument to resolve.
362+ /// - event: `CommandEvent` to resolve on.
363+ ///
364+ /// - throws:
365+ /// `CommandError.UnableToConvertArgument` when resolving fails.
366+ /// `CommandError.ArgumentNotFound` when no argument is found.
367+ ///
368+ /// - returns: The resolved argument.
307369 public func get< A> ( _ arg: A . Type , named name: String , on event: CommandEvent ) throws -> A . ResolvedArgument where A: CommandArgumentConvertible {
308370 guard let foundArg = self . arguments. first ( where: {
309371 $0. componentType == arg. typedName &&
0 commit comments