0001 // 0002 // HelpCommand.swift 0003 // Commandant 0004 // 0005 // Created by Justin Spahr-Summers on 2014-10-10. 0006 // Copyright (c) 2014 Carthage. All rights reserved. 0007 // 0008 0009 import Foundation 0010 import Result 0011 0012 /// A basic implementation of a `help` command, using information available in a 0013 /// `CommandRegistry`. 0014 /// 0015 /// If you want to use this command, initialize it with the registry, then add 0016 /// it to that same registry: 0017 /// 0018 /// let commands: CommandRegistry<MyErrorType> = … 0019 /// let helpCommand = HelpCommand(registry: commands) 0020 /// commands.register(helpCommand) 0021 public struct HelpCommand<ClientError: ErrorType>: CommandType { 0022 public typealias Options
HelpCommand.swift:22 public typealias Options = HelpOptions<ClientError>HelpCommand.swift:27 private let registry: CommandRegistry<ClientError>HelpCommand.swift:31 public init(registry: CommandRegistry<ClientError>) {= HelpOptions<ClientError> 0023 0024 public let verb = "help" 0025 public let function = "Display general or command-specific help" 0026 0027 private let registry
HelpCommand.swift:35 public func run(options: Options) -> Result<(), ClientError> {: CommandRegistry<ClientError> 0028 0029 /// Initializes the command to provide help from the given registry of 0030 /// commands. 0031 public init(registry: CommandRegistry<ClientError>) { 0032 self.registry = registry 0033 } 0034 0035 public func run(options: Options) -> Result<(), ClientError> { 0036 if let verb = options.verb { 0037 if let command = self.registry[verb] { 0038 print(command.function) 0039 if let usageError = command.usage() { 0040 print("\n\(usageError)") 0041 } 0042 return .Success(()) 0043 } else { 0044 fputs("Unrecognized command: '\(verb)'\n", stderr) 0045 } 0046 } 0047 0048 print("Available commands:\n") 0049 0050 let maxVerbLength = self.registry.commands.map { $0.verb.characters.count }.maxElement() ?? 0 0051 0052 for command in self.registry.commands { 0053 let padding = Repeat<Character>(count: maxVerbLength - command.verb.characters.count, repeatedValue: " ") 0054 print(" \(command.verb)\(String(padding)) \(command.function)") 0055 } 0056 0057 return .Success(()) 0058 } 0059 } 0060 0061 public struct HelpOptions
HelpCommand.swift:32 self.registry = registryHelpCommand.swift:50 let maxVerbLength = self.registry.commands.map { $0.verb.characters.count }.maxElement() ?? 0HelpCommand.swift:52 for command in self.registry.commands {<ClientError: ErrorType>: OptionsType { 0062 private let verb
HelpCommand.swift:22 public typealias Options = HelpOptions<ClientError>HelpCommand.swift:68 private static func create(verb: String) -> HelpOptions {: String? 0063 0064 private init
HelpCommand.swift:65 self.verb = verb(verb: String?) { 0065 self.verb = verb 0066 } 0067 0068 private static func create(verb: String) -> HelpOptions { 0069 return self.init(verb: (verb == "" ? nil : verb)) 0070 } 0071 0072 public static func evaluate(m: CommandMode) -> Result<HelpOptions, CommandantError<ClientError>> { 0073 return create 0074 <*> m <| Argument(defaultValue: "", usage: "the command to display help for") 0075 } 0076 } 0077
HelpCommand.swift:69 return self.init(verb: (verb == "" ? nil : verb))