Skip to content

Commit a4e4ae4

Browse files
authored
Update documentation and examples to use natural property wrapper syntax (#210)
Since #207, property wrapper usage with no arguments can now use the more natural syntax without the `()`. Updates documentation and example code accordingly.
1 parent 78e6cf9 commit a4e4ae4

File tree

12 files changed

+49
-84
lines changed

12 files changed

+49
-84
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ package updates, you can specify your package dependency using
3434

3535
```swift
3636
// old
37-
@Flag() var verbose: Bool
37+
@Flag var verbose: Bool
3838
// new
39-
@Flag() var verbose = false
39+
@Flag var verbose = false
4040
```
4141

4242
**_Important:_** There is a semantic change for flags with inversions that do
@@ -198,7 +198,7 @@ The 0.0.5 release includes contributions from [kennyyork], [natecook1000],
198198
properly constrained.
199199
- The parser no longer treats passing the same exclusive flag more than once as
200200
an error.
201-
- `ParsableArguments` types that are declared as `@OptionGroup()` properties on
201+
- `ParsableArguments` types that are declared as `@OptionGroup` properties on
202202
commands can now also be declared on subcommands. Previosuly, the parent
203203
command's declaration would prevent subcommands from seeing the user-supplied
204204
arguments.

Documentation/01 Getting Started.md

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ We'll define the initial version of the command as a type that conforms to the `
4646
import ArgumentParser
4747

4848
struct Count: ParsableCommand {
49-
@Argument()
50-
var inputFile: String
51-
52-
@Argument()
53-
var outputFile: String
49+
@Argument var inputFile: String
50+
@Argument var outputFile: String
5451

5552
mutating func run() throws {
5653
print("""
@@ -85,11 +82,8 @@ We do this by using the `@Option` property wrapper instead of `@Argument`:
8582

8683
```swift
8784
struct Count: ParsableCommand {
88-
@Option()
89-
var inputFile: String
90-
91-
@Option()
92-
var outputFile: String
85+
@Option var inputFile: String
86+
@Option var outputFile: String
9387

9488
mutating func run() throws {
9589
print("""
@@ -126,14 +120,9 @@ Let's change our `Count` type to look like this:
126120

127121
```swift
128122
struct Count: ParsableCommand {
129-
@Option()
130-
var inputFile: String
131-
132-
@Option()
133-
var outputFile: String
134-
135-
@Flag()
136-
var verbose = false
123+
@Option var inputFile: String
124+
@Option var outputFile: String
125+
@Flag var verbose = false
137126

138127
mutating func run() throws {
139128
if verbose {

Documentation/02 Arguments, Options, and Flags.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@ The three preceding examples could be calls of this `Example` command:
2626

2727
```swift
2828
struct Example: ParsableCommand {
29-
@Argument() var files: [String] = []
30-
31-
@Option() var count: Int?
32-
33-
@Option() var index = 0
34-
35-
@Flag() var verbose = false
36-
37-
@Flag() var stripWhitespace = false
29+
@Argument var files: [String] = []
30+
@Option var count: Int?
31+
@Option var index = 0
32+
@Flag var verbose = false
33+
@Flag var stripWhitespace = false
3834
}
3935
```
4036

@@ -49,11 +45,8 @@ Users must provide values for all properties with no implicit or specified defau
4945

5046
```swift
5147
struct Example: ParsableCommand {
52-
@Option()
53-
var userName: String
54-
55-
@Argument()
56-
var value: Int
48+
@Option var userName: String
49+
@Argument var value: Int
5750
}
5851
```
5952

@@ -74,9 +67,8 @@ When providing a default value for an array property, any user-supplied values r
7467

7568
```swift
7669
struct Lucky: ParsableCommand {
77-
@Argument()
78-
var numbers = [7, 14, 21]
79-
70+
@Argument var numbers = [7, 14, 21]
71+
8072
mutating func run() throws {
8173
print("""
8274
Your lucky numbers are:
@@ -154,7 +146,7 @@ struct Path: ExpressibleByArgument {
154146
}
155147

156148
struct Example: ParsableCommand {
157-
@Argument() var inputFile: Path
149+
@Argument var inputFile: Path
158150
}
159151
```
160152

@@ -166,7 +158,7 @@ enum ReleaseMode: String, ExpressibleByArgument {
166158
}
167159

168160
struct Example: ParsableCommand {
169-
@Option() var mode: ReleaseMode
161+
@Option var mode: ReleaseMode
170162

171163
mutating func run() throws {
172164
print(mode)
@@ -251,9 +243,8 @@ enum Color: String, EnumerableFlag {
251243
}
252244

253245
struct Example: ParsableCommand {
254-
@Flag() var cacheMethod: CacheMethod
255-
256-
@Flag() var colors: [Color] = []
246+
@Flag var cacheMethod: CacheMethod
247+
@Flag var colors: [Color] = []
257248

258249
mutating func run() throws {
259250
print(cacheMethod)
@@ -302,9 +293,9 @@ For example, this command defines a `--verbose` flag, a `--name` option, and an
302293

303294
```swift
304295
struct Example: ParsableCommand {
305-
@Flag() var verbose = false
306-
@Option() var name: String
307-
@Argument() var file: String?
296+
@Flag var verbose = false
297+
@Option var name: String
298+
@Argument var file: String?
308299

309300
mutating func run() throws {
310301
print("Verbose: \(verbose), name: \(name), file: \(file ?? "none")")
@@ -349,8 +340,8 @@ The default strategy for parsing options as arrays is to read each value from a
349340

350341
```swift
351342
struct Example: ParsableCommand {
352-
@Option() var file: [String] = []
353-
@Flag() var verbose = false
343+
@Option var file: [String] = []
344+
@Flag var verbose = false
354345

355346
mutating func run() throws {
356347
print("Verbose: \(verbose), files: \(file)")
@@ -400,8 +391,8 @@ The default strategy for parsing arrays of positional arguments is to ignore al
400391

401392
```swift
402393
struct Example: ParsableCommand {
403-
@Flag() var verbose = false
404-
@Argument() var files: [String] = []
394+
@Flag var verbose = false
395+
@Argument var files: [String] = []
405396

406397
mutating func run() throws {
407398
print("Verbose: \(verbose), files: \(files)")

Documentation/03 Commands and Subcommands.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ struct Options: ParsableArguments {
6363
}
6464
```
6565

66-
It's time to define our first two subcommands: `Add` and `Multiply`. Both of these subcommands include the arguments defined in the `Options` type by denoting that property with the `@OptionGroup()` property wrapper. `@OptionGroup` doesn't define any new arguments for a command; instead, it splats in the arguments defined by another `ParsableArguments` type.
66+
It's time to define our first two subcommands: `Add` and `Multiply`. Both of these subcommands include the arguments defined in the `Options` type by denoting that property with the `@OptionGroup` property wrapper. `@OptionGroup` doesn't define any new arguments for a command; instead, it splats in the arguments defined by another `ParsableArguments` type.
6767

6868
```swift
6969
extension Math {
7070
struct Add: ParsableCommand {
7171
static var configuration
7272
= CommandConfiguration(abstract: "Print the sum of the values.")
7373

74-
@OptionGroup()
75-
var options: Math.Options
74+
@OptionGroup var options: Math.Options
7675

7776
mutating func run() {
7877
let result = options.values.reduce(0, +)
@@ -84,8 +83,7 @@ extension Math {
8483
static var configuration
8584
= CommandConfiguration(abstract: "Print the product of the values.")
8685

87-
@OptionGroup()
88-
var options: Math.Options
86+
@OptionGroup var options: Math.Options
8987

9088
mutating func run() {
9189
let result = options.values.reduce(1, *)

Documentation/05 Validation and Errors.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ Here's a command that prints out one or more random elements from the list you p
1212

1313
```swift
1414
struct Select: ParsableCommand {
15-
@Option
16-
var count: Int = 1
17-
18-
@Argument()
19-
var elements: [String] = []
15+
@Option var count: Int = 1
16+
@Argument var elements: [String] = []
2017

2118
mutating func validate() throws {
2219
guard count >= 1 else {
@@ -64,7 +61,7 @@ The `ValidationError` type is a special `ArgumentParser` error — a validation
6461

6562
```swift
6663
struct LineCount: ParsableCommand {
67-
@Argument() var file: String
64+
@Argument var file: String
6865

6966
mutating func run() throws {
7067
let contents = try String(contentsOfFile: file, encoding: .utf8)
@@ -92,7 +89,7 @@ struct RuntimeError: Error, CustomStringConvertible {
9289
}
9390

9491
struct Example: ParsableCommand {
95-
@Argument() var inputFile: String
92+
@Argument var inputFile: String
9693

9794
mutating func run() throws {
9895
if !ExampleCore.processFile(inputFile) {

Documentation/06 Manual Parsing and Testing.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ Let's implement the `Select` command discussed in [Validation and Errors](05%20V
1212

1313
```swift
1414
struct SelectOptions: ParsableArguments {
15-
@Option()
16-
var count: Int = 1
17-
18-
@Argument()
19-
var elements: [String] = []
15+
@Option var count: Int = 1
16+
@Argument var elements: [String] = []
2017
}
2118
```
2219

Examples/math/main.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ extension Math {
5454

5555
// The `@OptionGroup` attribute includes the flags, options, and
5656
// arguments defined by another `ParsableArguments` type.
57-
@OptionGroup()
58-
var options: Options
57+
@OptionGroup var options: Options
5958

6059
mutating func run() {
6160
let result = options.values.reduce(0, +)
@@ -67,8 +66,7 @@ extension Math {
6766
static var configuration =
6867
CommandConfiguration(abstract: "Print the product of the values.")
6968

70-
@OptionGroup()
71-
var options: Options
69+
@OptionGroup var options: Options
7270

7371
mutating func run() {
7472
let result = options.values.reduce(1, *)

Sources/ArgumentParser/Parsable Properties/Argument.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ extension Argument where Value: ExpressibleByArgument {
104104
/// ```diff
105105
/// -@Argument(default: "bar")
106106
/// -var foo: String
107-
/// +@Argument()
108-
/// +var foo: String = "bar"
107+
/// +@Argument var foo: String = "bar"
109108
/// ```
110109
///
111110
/// - Parameters:
@@ -171,7 +170,7 @@ public enum ArgumentArrayParsingStrategy {
171170
/// For example, for a parsable type defined as following:
172171
///
173172
/// struct Options: ParsableArguments {
174-
/// @Flag() var verbose: Bool
173+
/// @Flag var verbose: Bool
175174
/// @Argument(parsing: .remaining) var words: [String]
176175
/// }
177176
///
@@ -188,7 +187,7 @@ public enum ArgumentArrayParsingStrategy {
188187
/// For example, for a parsable type defined as following:
189188
///
190189
/// struct Options: ParsableArguments {
191-
/// @Flag() var verbose: Bool
190+
/// @Flag var verbose: Bool
192191
/// @Argument(parsing: .unconditionalRemaining) var words: [String]
193192
/// }
194193
///

Sources/ArgumentParser/Parsable Properties/Option.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,8 @@ public enum ArrayParsingStrategy {
294294
/// through the terminator `--`. That is the more common approach. For example:
295295
/// ```swift
296296
/// struct Options: ParsableArguments {
297-
/// @Option()
298-
/// var name: String
299-
///
300-
/// @Argument()
301-
/// var remainder: [String]
297+
/// @Option var name: String
298+
/// @Argument var remainder: [String]
302299
/// }
303300
/// ```
304301
/// would parse the input `--name Foo -- Bar --baz` such that the `remainder`

Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/// }
2121
///
2222
/// struct Example: ParsableCommand {
23-
/// @Flag() var sizes: [Size]
23+
/// @Flag var sizes: [Size]
2424
///
2525
/// mutating func run() {
2626
/// print(sizes)

0 commit comments

Comments
 (0)