1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A type that can be parsed from a program's command-line arguments.
///
/// When you implement a `ParsableArguments` type, all properties must be declared with
/// one of the four property wrappers provided by the `ArgumentParser` library.
public protocol ParsableArguments: Decodable {
/// Creates an instance of this parsable type using the definitions
/// given by each property's wrapper.
init()
/// Validates the properties of the instance after parsing.
///
/// Implement this method to perform validation or other processing after
/// creating a new instance from command-line arguments.
mutating func validate() throws
/// The label to use for "Error: ..." messages from this type. (experimental)
static var _errorLabel: String { get }
}
/// A type that provides the `ParsableCommand` interface to a `ParsableArguments` type.
struct _WrappedParsableCommand<P: ParsableArguments>: ParsableCommand {
static var _commandName: String {
let name = String(describing: P.self).convertedToSnakeCase()
// If the type is named something like "TransformOptions", we only want
// to use "transform" as the command name.
if let optionsRange = name.range(of: "_options"),
optionsRange.upperBound == name.endIndex
{
return String(name[..<optionsRange.lowerBound])
} else {
return name
}
}
@OptionGroup var options: P
}
extension ParsableArguments {
public mutating func validate() throws {}
/// This type as-is if it conforms to `ParsableCommand`, or wrapped in the
/// `ParsableCommand` wrapper if not.
internal static var asCommand: ParsableCommand.Type {
self as? ParsableCommand.Type ?? _WrappedParsableCommand<Self>.self
}
public static var _errorLabel: String {
"Error"
}
}
// MARK: - API
extension ParsableArguments {
/// Parses a new instance of this type from command-line arguments.
///
/// - Parameter arguments: An array of arguments to use for parsing. If
/// `arguments` is `nil`, this uses the program's command-line arguments.
/// - Returns: A new instance of this type.
public static func parse(
_ arguments: [String]? = nil
) throws -> Self {
// Parse the command and unwrap the result if necessary.
switch try self.asCommand.parseAsRoot(arguments) {
case let helpCommand as HelpCommand:
throw ParserError.helpRequested(visibility: helpCommand.visibility)
case let result as _WrappedParsableCommand<Self>:
return result.options
case var result as Self:
do {
try result.validate()
} catch {
throw ParserError.userValidationError(error)
}
return result
default:
// TODO: this should be a "wrong command" message
throw ParserError.invalidState
}
}
/// Returns a brief message for the given error.
///
/// - Parameter error: An error to generate a message for.
/// - Returns: A message that can be displayed to the user.
public static func message(
for error: Error
) -> String {
MessageInfo(error: error, type: self).message
}
/// Returns a full message for the given error, including usage information,
/// if appropriate.
///
/// - Parameter error: An error to generate a message for.
/// - Returns: A message that can be displayed to the user.
public static func fullMessage(
for error: Error
) -> String {
MessageInfo(error: error, type: self).fullText(for: self)
}
/// Returns the text of the help screen for this type.
///
/// - Parameters:
/// - columns: The column width to use when wrapping long line in the
/// help screen. If `columns` is `nil`, uses the current terminal
/// width, or a default value of `80` if the terminal width is not
/// available.
/// - Returns: The full help screen for this type.
@_disfavoredOverload
@available(*, deprecated, message: "Use helpMessage(includeHidden:columns:) instead.")
public static func helpMessage(
columns: Int?
) -> String {
helpMessage(includeHidden: false, columns: columns)
}
/// Returns the text of the help screen for this type.
///
/// - Parameters:
/// - includeHidden: Include hidden help information in the generated
/// message.
/// - columns: The column width to use when wrapping long line in the
/// help screen. If `columns` is `nil`, uses the current terminal
/// width, or a default value of `80` if the terminal width is not
/// available.
/// - Returns: The full help screen for this type.
public static func helpMessage(
includeHidden: Bool = false,
columns: Int? = nil
) -> String {
HelpGenerator(self, visibility: includeHidden ? .hidden : .default)
.rendered(screenWidth: columns)
}
/// Returns the JSON representation of this type.
public static func _dumpHelp() -> String {
DumpHelpGenerator(self).rendered()
}
/// Returns the exit code for the given error.
///
/// The returned code is the same exit code that is used if `error` is passed
/// to `exit(withError:)`.
///
/// - Parameter error: An error to generate an exit code for.
/// - Returns: The exit code for `error`.
public static func exitCode(
for error: Error
) -> ExitCode {
MessageInfo(error: error, type: self).exitCode
}
/// Returns a shell completion script for the specified shell.
///
/// - Parameter shell: The shell to generate a completion script for.
/// - Returns: The completion script for `shell`.
public static func completionScript(for shell: CompletionShell) -> String {
let completionsGenerator = try! CompletionsGenerator(command: self.asCommand, shell: shell)
return completionsGenerator.generateCompletionScript()
}
/// Terminates execution with a message and exit code that is appropriate
/// for the given error.
///
/// If the `error` parameter is `nil`, this method prints nothing and exits
/// with code `EXIT_SUCCESS`. If `error` represents a help request or
/// another `CleanExit` error, this method prints help information and
/// exits with code `EXIT_SUCCESS`. Otherwise, this method prints a relevant
/// error message and exits with code `EX_USAGE` or `EXIT_FAILURE`.
///
/// - Parameter error: The error to use when exiting, if any.
public static func exit(
withError error: Error? = nil
) -> Never {
guard let error = error else {
Platform.exit(ExitCode.success.rawValue)
}
let messageInfo = MessageInfo(error: error, type: self)
let fullText = messageInfo.fullText(for: self)
if !fullText.isEmpty {
if messageInfo.shouldExitCleanly {
print(fullText)
} else {
print(fullText, to: &Platform.standardError)
}
}
Platform.exit(messageInfo.exitCode.rawValue)
}
/// Parses a new instance of this type from command-line arguments or exits
/// with a relevant message.
///
/// - Parameter arguments: An array of arguments to use for parsing. If
/// `arguments` is `nil`, this uses the program's command-line arguments.
public static func parseOrExit(
_ arguments: [String]? = nil
) -> Self {
do {
return try parse(arguments)
} catch {
exit(withError: error)
}
}
}
/// Unboxes the given value if it is a `nil` value.
///
/// If the value passed is the `.none` case of any optional type, this function
/// returns `nil`.
///
/// let intAsAny = (1 as Int?) as Any
/// let nilAsAny = (nil as Int?) as Any
/// nilOrValue(intAsAny) // Optional(1) as Any?
/// nilOrValue(nilAsAny) // nil as Any?
func nilOrValue(_ value: Any) -> Any? {
if case Optional<Any>.none = value {
return nil
} else {
return value
}
}
/// Existential protocol for property wrappers, so that they can provide
/// the argument set that they define.
protocol ArgumentSetProvider {
func argumentSet(for key: InputKey) -> ArgumentSet
var _visibility: ArgumentVisibility { get }
}
extension ArgumentSetProvider {
var _visibility: ArgumentVisibility { .default }
}
extension ArgumentSet {
init(_ type: ParsableArguments.Type, visibility: ArgumentVisibility, parent: InputKey?) {
#if DEBUG
do {
try type._validate(parent: parent)
} catch {
assertionFailure("\(error)")
}
#endif
let a: [ArgumentSet] = Mirror(reflecting: type.init())
.children
.compactMap { child -> ArgumentSet? in
guard let codingKey = child.label else { return nil }
if let parsed = child.value as? ArgumentSetProvider {
guard parsed._visibility.isAtLeastAsVisible(as: visibility)
else { return nil }
let key = InputKey(name: codingKey, parent: parent)
return parsed.argumentSet(for: key)
} else {
let arg = ArgumentDefinition(
unparsedKey: codingKey,
default: nilOrValue(child.value), parent: parent)
// Save a non-wrapped property as is
return ArgumentSet(arg)
}
}
self.init(
a.joined().filter { $0.help.visibility.isAtLeastAsVisible(as: visibility) })
}
}
/// The fatal error message to display when someone accesses a
/// `ParsableArguments` type after initializing it directly.
internal let directlyInitializedError = """
--------------------------------------------------------------------
Can't read a value from a parsable argument definition.
This error indicates that a property declared with an `@Argument`,
`@Option`, `@Flag`, or `@OptionGroup` property wrapper was neither
initialized to a value nor decoded from command-line arguments.
To get a valid value, either call one of the static parsing methods
(`parse`, `parseAsRoot`, or `main`) or define an initializer that
initializes _every_ property of your parsable type.
--------------------------------------------------------------------
"""
|