0001 // 0002 // Router.swift 0003 // SwiftCLI 0004 // 0005 // Created by Jake Heiser on 7/11/14. 0006 // Copyright (c) 2014 jakeheis. All rights reserved. 0007 // 0008 0009 public class Router{ 0010 0011 /// Optional configuration for the Router 0012 public struct Config
CLI.swift:23 public static var routerConfig: Router.Config?CLI.swift:140 let router = Router(commands: allCommands, arguments: arguments, defaultCommand: defaultCommand, config: routerConfig)Router.swift:55 throw Router.ArgumentErrorRouter.swift:72 throw Router.CommandNotFoundError{ 0013 0014 /// Allow shortcut flags to be routed to commands; e.g. route -h to the HelpCommand 0015 public var enableShortcutRouting
CLI.swift:23 public static var routerConfig: Router.Config?Router.swift:28 private var config: ConfigRouter.swift:33 init(commands: [CommandType], arguments: RawArguments, defaultCommand: CommandType, config: Config?) {Router.swift:38 self.config = config ?? Config(): Bool = true 0016 0017 init
Router.swift:20 self.enableShortcutRouting = enableShortcutRoutingRouter.swift:60 where config.enableShortcutRouting {() {} 0018 0019 init(enableShortcutRouting: Bool) { 0020 self.enableShortcutRouting = enableShortcutRouting 0021 } 0022 } 0023 0024 private let commands
Router.swift:38 self.config = config ?? Config(): [CommandType] 0025 private let arguments
Router.swift:34 self.commands = commandsRouter.swift:59 if let shortcutCommand = commands.filter({ $0.commandShortcut == commandSearchName }).firstRouter.swift:67 command = commands.filter { $0.commandName == commandSearchName }.first: RawArguments 0026 private let defaultCommand
Router.swift:35 self.arguments = argumentsRouter.swift:42 guard arguments.unclassifiedArguments().count > 0 else {Router.swift:54 guard let commandSearchName = arguments.firstArgumentOfType(.Unclassified) else {Router.swift:62 arguments.classifyArgument(argument: commandSearchName, type: .CommandName)Router.swift:68 arguments.classifyArgument(argument: commandSearchName, type: .CommandName): CommandType 0027 0028 private var config
Router.swift:36 self.defaultCommand = defaultCommandRouter.swift:43 return defaultCommandRouter.swift:64 command = defaultCommand: Config 0029 0030 static let CommandNotFoundError
Router.swift:38 self.config = config ?? Config()Router.swift:60 where config.enableShortcutRouting {= CLIError.Error("Command not found") 0031 static let ArgumentError
Router.swift:72 throw Router.CommandNotFoundError= CLIError.Error("Router failed") 0032 0033 init
Router.swift:55 throw Router.ArgumentError(commands: [CommandType], arguments: RawArguments, defaultCommand: CommandType, config: Config?) { 0034 self.commands = commands 0035 self.arguments = arguments 0036 self.defaultCommand = defaultCommand 0037 0038 self.config = config ?? Config() 0039 } 0040 0041 func route
CLI.swift:140 let router = Router(commands: allCommands, arguments: arguments, defaultCommand: defaultCommand, config: routerConfig)() throws -> CommandType { 0042 guard arguments.unclassifiedArguments().count > 0 else { 0043 return defaultCommand 0044 } 0045 0046 return try findCommand() 0047 } 0048 0049 // MARK: - Privates 0050 0051 private func findCommand
CLI.swift:141 return try router.route()() throws -> CommandType { 0052 var command: CommandType? 0053 0054 guard let commandSearchName = arguments.firstArgumentOfType(.Unclassified) else { 0055 throw Router.ArgumentError 0056 } 0057 0058 if commandSearchName.hasPrefix("-") { 0059 if let shortcutCommand = commands.filter({ $0.commandShortcut == commandSearchName }).first 0060 where config.enableShortcutRouting { 0061 command = shortcutCommand 0062 arguments.classifyArgument(argument: commandSearchName, type: .CommandName) 0063 } else { 0064 command = defaultCommand 0065 } 0066 } else { 0067 command = commands.filter { $0.commandName == commandSearchName }.first 0068 arguments.classifyArgument(argument: commandSearchName, type: .CommandName) 0069 } 0070 0071 guard let foundCommand = command else { 0072 throw Router.CommandNotFoundError 0073 } 0074 0075 return foundCommand 0076 } 0077 0078 }
Router.swift:46 return try findCommand()