Before

…lines 1–5 unchanged (5 lines)
6// Licensed under Apache License v2.0 with Runtime Library Exception
7//
8// See https://swift.org/LICENSE.txt for license information
9//
10//===----------------------------------------------------------------------===//
11
12#if compiler(>=6.0)
13internal import Foundation
14#else
15import Foundation
16#endif
17
18/// A type that can be parsed from a program's command-line arguments.
19///
20/// When you implement a `ParsableArguments` type, all properties must be declared with
21/// one of the four property wrappers provided by the `ArgumentParser` library.
22public protocol ParsableArguments: Decodable, _SendableMetatype {
…lines 23–37 unchanged (15 lines)
38struct _WrappedParsableCommand<P: ParsableArguments>: ParsableCommand {
39 static var _commandName: String {
40 let name = String(describing: P.self).convertedToSnakeCase()
41
42 // If the type is named something like "TransformOptions", we only want
43 // to use "transform" as the command name.
44 if let optionsRange = name.range(of: "_options"),
45 optionsRange.upperBound == name.endIndex
46 {
47 return String(name[..<optionsRange.lowerBound])
48 } else {
49 return name
50 }
51 }
52
53 @OptionGroup var options: P
54}
55
56extension ParsableArguments {
57 public mutating func validate() throws {}
…lines 58–357 unchanged (300 lines)

After

…lines 1–4 unchanged (4 lines)
5// Copyright (c) 2020 Apple Inc. and the Swift project authors
6// Licensed under Apache License v2.0 with Runtime Library Exception
7//
8// See https://swift.org/LICENSE.txt for license information
9//
10//===----------------------------------------------------------------------===//
11
12/// A type that can be parsed from a program's command-line arguments.
13///
14/// When you implement a `ParsableArguments` type, all properties must be declared with
15/// one of the four property wrappers provided by the `ArgumentParser` library.
16public protocol ParsableArguments: Decodable, _SendableMetatype {
17 /// Creates an instance of this parsable type using the definitions
…lines 18–31 unchanged (14 lines)
32struct _WrappedParsableCommand<P: ParsableArguments>: ParsableCommand {
33 static var _commandName: String {
34 let name = String(describing: P.self).convertedToSnakeCase()
35
36 // If the type is named something like "TransformOptions", we only want
37 // to use "transform" as the command name.
38 guard
39 let matchRange = name.firstMatch(of: "_options", at: name.startIndex),
40 matchRange.end == name.endIndex
41 else {
42 return name
43 }
44
45 return String(name[..<matchRange.start])
46 }
47
48 @OptionGroup var options: P
49}
50
51extension ParsableArguments {
…lines 52–352 unchanged (301 lines)

Before

source_file
comment "//===-------------------------------------------------------"...
comment "//"
comment "// This source file is part of the Swift Argument Parser ope"...
comment "//"
comment "// Copyright (c) 2020 Apple Inc. and the Swift project autho"...
comment "// Licensed under Apache License v2.0 with Runtime Library E"...
comment "//"
comment "// See https://swift.org/LICENSE.txt for license information"
comment "//"
comment "//===-------------------------------------------------------"...
directive
#if "#if"
compiler "compiler"
( "("
>= ">="
integer_literal "6"
. "."
integer_literal "0"
) ")"
import_declaration
modifiers
visibility_modifier
internal "internal"
import "import"
identifier
simple_identifier "Foundation"
directive
#else "#else"
import_declaration
import "import"
identifier
simple_identifier "Foundation"
directive
#endif "#endif"
comment "/// A type that can be parsed from a program's command-line "...
comment "///"
comment "/// When you implement a `ParsableArguments` type, all prope"...
comment "/// one of the four property wrappers provided by the `Argum"...
protocol_declaration (+54 nodes collapsed)
comment "/// A type that provides the `ParsableCommand` interface to "...
class_declaration
struct "struct"
type_identifier "_WrappedParsableCommand"
type_parameters
< "<"
type_parameter
type_identifier "P"
: ":"
user_type
type_identifier "ParsableArguments"
> ">"
: ":"
inheritance_specifier
user_type
type_identifier "ParsableCommand"
class_body
{ "{"
property_declaration
modifiers
property_modifier
static "static"
value_binding_pattern
var "var"
pattern
simple_identifier "_commandName"
type_annotation
: ":"
user_type
type_identifier "String"
computed_property
{ "{"
statements
property_declaration (+30 nodes collapsed)
comment "// If the type is named something like \"TransformOptions\", w"...
comment "// to use \"transform\" as the command name."
if_statement
if "if"
value_binding_pattern
let "let"
simple_identifier "optionsRange"
= "="
call_expression
navigation_expression
simple_identifier "name"
navigation_suffix
. "."
simple_identifier "range"
call_suffix
value_arguments
( "("
value_argument
value_argument_label
simple_identifier "of"
: ":"
line_string_literal
" "\""
line_str_text "_options"
" "\""
) ")"
, ","
equality_expression
navigation_expression
simple_identifier "optionsRange"
navigation_suffix
. "."
simple_identifier "upperBound"
== "=="
navigation_expression
simple_identifier "name"
navigation_suffix
. "."
simple_identifier "endIndex"
{ "{"
statements
control_transfer_statement
return "return"
call_expression
simple_identifier "String"
call_suffix
value_arguments
( "("
value_argument
call_expression
simple_identifier "name"
call_suffix
value_arguments
[ "["
value_argument
open_start_range_expression
..< "..<"
navigation_expression
simple_identifier "optionsRange"
navigation_suffix
. "."
simple_identifier "lowerBound"
] "]"
) ")"
} "}"
else "else"
{ "{"
statements
control_transfer_statement
return "return"
simple_identifier "name"
} "}"
} "}"
property_declaration
modifiers
attribute
@ "@"
user_type
type_identifier "OptionGroup"
value_binding_pattern
var "var"
pattern
simple_identifier "options"
type_annotation
: ":"
user_type
type_identifier "P"
} "}"
class_declaration (+88 nodes collapsed)
comment "// MARK: - API"
class_declaration (+1064 nodes collapsed)
comment "/// Unboxes the given value if it is a `nil` value."
comment "///"
comment "/// If the value passed is the `.none` case of any optional "...
comment "/// returns `nil`."
comment "///"
comment "/// let intAsAny = (1 as Int?) as Any"
comment "/// let nilAsAny = (nil as Int?) as Any"
comment "/// nilOrValue(intAsAny) // Optional(1) as Any?"
comment "/// nilOrValue(nilAsAny) // nil as Any?"
function_declaration (+48 nodes collapsed)
comment "/// Existential protocol for property wrappers, so that they"...
comment "/// the argument set that they define."
protocol_declaration (+34 nodes collapsed)
class_declaration (+23 nodes collapsed)
class_declaration (+358 nodes collapsed)
comment "/// Prints the given message to standard error and exits wit"...
comment "///"
comment "/// - Parameter message: The message to print to standard er"...
comment "/// should be pre-wrapped, if desired."
function_declaration (+169 nodes collapsed)
comment "/// The fatal error message to display when someone accesses"...
comment "/// `ParsableArguments` type after initializing it directly."
property_declaration
modifiers
visibility_modifier
internal "internal"
value_binding_pattern
let "let"
pattern
simple_identifier "directlyInitializedError"
= "="
multi_line_string_literal
""" "\"\"\""
multi_line_str_text "\n Can't read a value from a parsable\u{a0}argument definition.\n\n"...
""" "\"\"\""

After

source_file
comment "//===-------------------------------------------------------"...
comment "//"
comment "// This source file is part of the Swift Argument Parser ope"...
comment "//"
comment "// Copyright (c) 2020 Apple Inc. and the Swift project autho"...
comment "// Licensed under Apache License v2.0 with Runtime Library E"...
comment "//"
comment "// See https://swift.org/LICENSE.txt for license information"
comment "//"
comment "//===-------------------------------------------------------"...
comment "/// A type that can be parsed from a program's command-line "...
comment "///"
comment "/// When you implement a `ParsableArguments` type, all prope"...
comment "/// one of the four property wrappers provided by the `Argum"...
protocol_declaration (+54 nodes collapsed)
comment "/// A type that provides the `ParsableCommand` interface to "...
class_declaration
struct "struct"
type_identifier "_WrappedParsableCommand"
type_parameters
< "<"
type_parameter
type_identifier "P"
: ":"
user_type
type_identifier "ParsableArguments"
> ">"
: ":"
inheritance_specifier
user_type
type_identifier "ParsableCommand"
class_body
{ "{"
property_declaration
modifiers
property_modifier
static "static"
value_binding_pattern
var "var"
pattern
simple_identifier "_commandName"
type_annotation
: ":"
user_type
type_identifier "String"
computed_property
{ "{"
statements
property_declaration (+30 nodes collapsed)
comment "// If the type is named something like \"TransformOptions\", w"...
comment "// to use \"transform\" as the command name."
guard_statement
guard "guard"
value_binding_pattern
let "let"
simple_identifier "matchRange"
= "="
call_expression
navigation_expression
simple_identifier "name"
navigation_suffix
. "."
simple_identifier "firstMatch"
call_suffix
value_arguments
( "("
value_argument
value_argument_label
simple_identifier "of"
: ":"
line_string_literal
" "\""
line_str_text "_options"
" "\""
, ","
value_argument
value_argument_label
simple_identifier "at"
: ":"
navigation_expression
simple_identifier "name"
navigation_suffix
. "."
simple_identifier "startIndex"
) ")"
, ","
equality_expression
navigation_expression
simple_identifier "matchRange"
navigation_suffix
. "."
simple_identifier "end"
== "=="
navigation_expression
simple_identifier "name"
navigation_suffix
. "."
simple_identifier "endIndex"
else "else"
{ "{"
statements
control_transfer_statement
return "return"
simple_identifier "name"
} "}"
control_transfer_statement
return "return"
call_expression
simple_identifier "String"
call_suffix
value_arguments
( "("
value_argument
call_expression
simple_identifier "name"
call_suffix
value_arguments
[ "["
value_argument
open_start_range_expression
..< "..<"
navigation_expression
simple_identifier "matchRange"
navigation_suffix
. "."
simple_identifier "start"
] "]"
) ")"
} "}"
property_declaration
modifiers
attribute
@ "@"
user_type
type_identifier "OptionGroup"
value_binding_pattern
var "var"
pattern
simple_identifier "options"
type_annotation
: ":"
user_type
type_identifier "P"
} "}"
class_declaration (+88 nodes collapsed)
comment "// MARK: - API"
class_declaration (+1064 nodes collapsed)
comment "/// Unboxes the given value if it is a `nil` value."
comment "///"
comment "/// If the value passed is the `.none` case of any optional "...
comment "/// returns `nil`."
comment "///"
comment "/// let intAsAny = (1 as Int?) as Any"
comment "/// let nilAsAny = (nil as Int?) as Any"
comment "/// nilOrValue(intAsAny) // Optional(1) as Any?"
comment "/// nilOrValue(nilAsAny) // nil as Any?"
function_declaration (+48 nodes collapsed)
comment "/// Existential protocol for property wrappers, so that they"...
comment "/// the argument set that they define."
protocol_declaration (+34 nodes collapsed)
class_declaration (+23 nodes collapsed)
class_declaration (+358 nodes collapsed)
comment "/// Prints the given message to standard error and exits wit"...
comment "///"
comment "/// - Parameter message: The message to print to standard er"...
comment "/// should be pre-wrapped, if desired."
function_declaration (+169 nodes collapsed)
comment "/// The fatal error message to display when someone accesses"...
comment "/// `ParsableArguments` type after initializing it directly."
property_declaration
modifiers
visibility_modifier
internal "internal"
value_binding_pattern
let "let"
pattern
simple_identifier "directlyInitializedError"
= "="
multi_line_string_literal
""" "\"\"\""
multi_line_str_text "\n Can't read a value from a parsable\u{a0}argument definition.\n\n"...
""" "\"\"\""