0001 // 0002 // Switch.swift 0003 // Commandant 0004 // 0005 // Created by Neil Pankey on 3/31/15. 0006 // Copyright (c) 2015 Carthage. All rights reserved. 0007 // 0008 0009 import Result 0010 0011 /// Describes a parameterless command line flag that defaults to false and can only 0012 /// be switched on. Canonical examples include `--force` and `--recurse`. 0013 /// 0014 /// For a boolean toggle that can be enabled and disabled use `Option<Bool>`. 0015 public struct Switch{ 0016 /// The key that enables this switch. For example, a key of `verbose` would be 0017 /// used for a `--verbose` option. 0018 public let key
Switch.swift:38 extension Switch: CustomStringConvertible {Switch.swift:52 public func <| <ClientError> (mode: CommandMode, option: Switch) -> Result<Bool, CommandantError<ClientError>> {: String 0019 0020 /// Optional single letter flag that enables this switch. For example, `-v` would 0021 /// be used as a shorthand for `--verbose`. 0022 /// 0023 /// Multiple flags can be grouped together as a single argument and will split 0024 /// when parsing (e.g. `rm -rf` treats 'r' and 'f' as inidividual flags). 0025 public let flag
Switch.swift:33 self.key = keySwitch.swift:40 var options = "--\(key)"Switch.swift:55 var enabled = arguments.consumeKey(option.key): Character? 0026 0027 /// A human-readable string describing the purpose of this option. This will 0028 /// be shown in help messages. 0029 public let usage
Switch.swift:32 self.flag = flagSwitch.swift:41 if let flag = self.flag {Switch.swift:56 if let flag = option.flag {: String 0030 0031 public init(flag: Character? = nil, key: String, usage: String) { 0032 self.flag = flag 0033 self.key = key 0034 self.usage = usage 0035 } 0036 } 0037 0038 extension Switch: CustomStringConvertible { 0039 public var description: String { 0040 var options = "--\(key)" 0041 if let flag = self.flag { 0042 options += "|-\(flag)" 0043 } 0044 return options 0045 } 0046 } 0047 0048 /// Evaluates the given boolean switch in the given mode. 0049 /// 0050 /// If parsing command line arguments, and no value was specified on the command 0051 /// line, the option's `defaultValue` is used. 0052 public func <| <ClientError> (mode: CommandMode, option: Switch) -> Result<Bool, CommandantError<ClientError>> { 0053 switch mode { 0054 case let .Arguments(arguments): 0055 var enabled = arguments.consumeKey(option.key) 0056 if let flag = option.flag { 0057 enabled = arguments.consumeBooleanFlag(flag) 0058 } 0059 return .Success(enabled) 0060 0061 case .Usage: 0062 return .Failure(informativeUsageError(option.description, usage: option.usage)) 0063 } 0064 } 0065
Switch.swift:34 self.usage = usage