0001    //
0002    //  CLI.swift
0003    //  SwiftCLI
0004    //
0005    //  Created by Jake Heiser on 7/20/14.
0006    //  Copyright (c) 2014 jakeheis. All rights reserved.
0007    //
0008    
0009    public class CLI
CLI.swift:21
    public static var defaultCommand: CommandType = helpCommand!
CommandMessageGenerator.swift:12
        var message = "Usage: \(CLI.appName)"
HelpCommand.swift:34
        print("\(CLI.appDescription)\n")
VersionCommand.swift:28
        print("Version: \(CLI.appVersion)")
{ 0010 0011 // MARK: - Information 0012 0013 public static var appName
CLI.swift:35
        appName = name
CommandMessageGenerator.swift:12
        var message = "Usage: \(CLI.appName)"
= "" 0014 public static var appVersion
CLI.swift:38
            appVersion = version
VersionCommand.swift:28
        print("Version: \(CLI.appVersion)")
= "1.0" 0015 public static var appDescription
CLI.swift:42
            appDescription = description
HelpCommand.swift:34
        print("\(CLI.appDescription)\n")
= "" 0016 0017 private static var commands
CLI.swift:55
        commands.append(command)
CLI.swift:131
        var allCommands = commands
CLI.swift:133
            hc.allCommands = commands
: [CommandType] = [] 0018 0019 public static var helpCommand
CLI.swift:21
    public static var defaultCommand: CommandType = helpCommand!
CLI.swift:132
        if let hc = helpCommand {
: HelpCommand? = HelpCommand() 0020 public static var versionComand
CLI.swift:136
        if let vc = versionComand {
: CommandType? = VersionCommand() 0021 public static var defaultCommand
CLI.swift:140
        let router = Router(commands: allCommands, arguments: arguments, defaultCommand: defaultCommand, config: routerConfig)        
: CommandType = helpCommand! 0022 0023 public static var routerConfig
CLI.swift:140
        let router = Router(commands: allCommands, arguments: arguments, defaultCommand: defaultCommand, config: routerConfig)        
: Router.Config? 0024 0025 // MARK: - Setup 0026 0027 /** 0028 Sets the CLI up with basic information 0029 0030 - Parameter name: name of the app, printed in the help message and command usage statements 0031 - Parameter version: version of the app, printed by the VersionCommand 0032 - Parameter description: description of the app, printed in the help message 0033 */ 0034 public class func setup(name name: String, version: String? = nil, description: String? = nil) { 0035 appName = name 0036 0037 if let version = version { 0038 appVersion = version 0039 } 0040 0041 if let description = description { 0042 appDescription = description 0043 } 0044 0045 Input.checkForPipedData() 0046 } 0047 0048 /** 0049 Registers a command with the CLI for routing and execution. All commands must be registered 0050 with this method or its siblings before calling `CLI.go()` 0051 0052 - Parameter command: the command to be registered 0053 */ 0054 public class func registerCommand
CLI.swift:65
        commands.each { self.registerCommand($0) }
CLI.swift:76
        registerCommand(chainable)
(command: CommandType) { 0055 commands.append(command) 0056 } 0057 0058 /** 0059 Registers a group of commands with the CLI for routing and execution. All commands must be registered 0060 with this method or its siblings before calling `CLI.go()` 0061 0062 - Parameter commands: the commands to be registered 0063 */ 0064 public class func registerCommands(commands: [CommandType]) { 0065 commands.each { self.registerCommand($0) } 0066 } 0067 0068 /** 0069 Registers a chainable command with the CLI for routing and execution. 0070 0071 - Parameter commandName: the name of the new chainable command 0072 - Returns: a new chainable command for immediate chaining 0073 */ 0074 public class func registerChainableCommand(commandName commandName: String) -> ChainableCommand { 0075 let chainable = ChainableCommand(commandName: commandName) 0076 registerCommand(chainable) 0077 return chainable 0078 } 0079 0080 // MARK: - Go 0081 0082 /** 0083 Kicks off the entire CLI process, routing to and executing the command specified by the passed arguments. 0084 Uses the arguments passed in the command line. 0085 0086 - SeeAlso: `debugGoWithArgumentString()` when debugging 0087 - Returns: a CLIResult (Int) representing the success of the CLI in routing to and executing the correct 0088 command. Usually should be passed to `exit(result)` 0089 */ 0090 public class func go() -> CLIResult { 0091 return goWithArguments(RawArguments()) 0092 } 0093 0094 /** 0095 Kicks off the entire CLI process, routing to and executing the command specified by the passed arguments. 0096 Uses the arguments passed in as an argument. 0097 0098 - Parameter argumentString: the arguments to use when running the CLI 0099 - SeeAlso: `go()` when running from the command line 0100 - Returns: a CLIResult (Int) representing the success of the CLI in routing to and executing the correct 0101 command. Usually should be passed to `exit(result)` 0102 */ 0103 public class func debugGoWithArgumentString(argumentString: String) -> CLIResult { 0104 print("[Debug Mode]") 0105 return goWithArguments(RawArguments(argumentString: argumentString)) 0106 } 0107 0108 private class func goWithArguments
CLI.swift:91
       return goWithArguments(RawArguments())
CLI.swift:105
        return goWithArguments(RawArguments(argumentString: argumentString))
(arguments: RawArguments) -> CLIResult { 0109 do { 0110 let command = try routeCommand(arguments: arguments) 0111 let result = try setupOptionsAndArguments(command, arguments: arguments) 0112 if let arguments = result.arguments where result.execute { 0113 try command.execute(arguments) 0114 } 0115 0116 return CLIResult.Success 0117 } catch CLIError.Error(let error) { 0118 printError(error) 0119 } catch CLIError.EmptyError { 0120 // Do nothing 0121 } catch _ { 0122 printError("An error occurred") 0123 } 0124 0125 return CLIResult.Error 0126 } 0127 0128 // MARK: - Privates 0129 0130 class private func routeCommand
CLI.swift:110
            let command = try routeCommand(arguments: arguments)
(arguments arguments: RawArguments) throws -> CommandType { 0131 var allCommands = commands 0132 if let hc = helpCommand { 0133 hc.allCommands = commands 0134 allCommands.append(hc) 0135 } 0136 if let vc = versionComand { 0137 allCommands.append(vc) 0138 } 0139 0140 let router = Router(commands: allCommands, arguments: arguments, defaultCommand: defaultCommand, config: routerConfig) 0141 return try router.route() 0142 } 0143 0144 class private func setupOptionsAndArguments
CLI.swift:111
            let result = try setupOptionsAndArguments(command, arguments: arguments)
(command: CommandType, arguments: RawArguments) throws -> (execute: Bool, arguments: CommandArguments?) { 0145 if let optionCommand = command as? OptionCommandType { 0146 let options = Options() 0147 0148 optionCommand.internalSetupOptions(options) 0149 options.recognizeOptionsInArguments(arguments) 0150 0151 if options.exitEarly { // True if -h flag given (show help but exit early before executing command) 0152 return (false, nil) 0153 } 0154 0155 if options.misusedOptionsPresent() { 0156 if let message = CommandMessageGenerator.generateMisusedOptionsStatement(command: optionCommand, options: options) { 0157 printError(message) 0158 } 0159 if optionCommand.failOnUnrecognizedOptions { 0160 throw CLIError.EmptyError 0161 } 0162 } 0163 } 0164 0165 let commandSignature = CommandSignature(command.commandSignature) 0166 0167 return (true, try CommandArguments.fromRawArguments(arguments, signature: commandSignature)) 0168 } 0169 0170 } 0171 0172 // MARK: - 0173 0174 public enum CLIError
CLI.swift:160
                    throw CLIError.EmptyError
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))
CommandArguments.swift:87
            throw CLIError.Error("Expected no arguments, got \(rawArguments.unclassifiedArguments().count).")
Input.swift:17
    static let PipeUserInputOverlapError = CLIError.Error("Data should not be both piped and input")
Router.swift:30
    static let CommandNotFoundError = CLIError.Error("Command not found")
Router.swift:31
    static let ArgumentError = CLIError.Error("Router failed")
: ErrorType { 0175 case Error
CLI.swift:117
        } catch CLIError.Error(let error) {
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))
CommandArguments.swift:87
            throw CLIError.Error("Expected no arguments, got \(rawArguments.unclassifiedArguments().count).")
Input.swift:17
    static let PipeUserInputOverlapError = CLIError.Error("Data should not be both piped and input")
Router.swift:30
    static let CommandNotFoundError = CLIError.Error("Command not found")
Router.swift:31
    static let ArgumentError = CLIError.Error("Router failed")
(String) 0176 case EmptyError
CLI.swift:119
        } catch CLIError.EmptyError {
CLI.swift:160
                    throw CLIError.EmptyError
0177 } 0178 0179 public typealias CLIResult
CLI.swift:90
    public class func go() -> CLIResult {
CLI.swift:103
    public class func debugGoWithArgumentString(argumentString: String) -> CLIResult {
CLI.swift:108
    private class func goWithArguments(arguments: RawArguments) -> CLIResult {
CLI.swift:116
            return CLIResult.Success
CLI.swift:125
        return CLIResult.Error
CLI.swift:181
extension CLIResult {
CLI.swift:183
    static var Success: CLIResult {
CLI.swift:187
    static var Error: CLIResult {
= Int32 0180 0181 extension CLIResult { 0182 0183 static var Success
CLI.swift:116
            return CLIResult.Success
: CLIResult { 0184 return 0 0185 } 0186 0187 static var Error
CLI.swift:125
        return CLIResult.Error
: CLIResult { 0188 return 1 0189 } 0190 0191 }