You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Use type inference for flags / options
* Use default value syntax for arg/option arrays
* Allow a default for flag arrays
* Fix some whitespace
* Allow arguments validations to warn instead of fail
* Move nonsense flag warning to argument validation
* Update guides/readme with default literal syntax
Copy file name to clipboardExpand all lines: Documentation/02 Arguments, Options, and Flags.md
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,24 +26,24 @@ The three preceding examples could be calls of this `Example` command:
26
26
27
27
```swift
28
28
structExample: ParsableCommand {
29
-
@Argument() var files: [String]
29
+
@Argument() var files: [String]= []
30
30
31
31
@Option() var count: Int?
32
32
33
-
@Optionvar index: Int=0
33
+
@Option()var index =0
34
34
35
-
@Flag() var verbose: Bool
35
+
@Flag() var verbose=false
36
36
37
-
@Flag() var stripWhitespace: Bool
37
+
@Flag() var stripWhitespace=false
38
38
}
39
39
```
40
40
41
41
This example shows how `ArgumentParser` provides defaults that speed up your initial development process:
42
42
43
43
- Option and flag names are derived from the names of your command's properties.
44
-
-Whether arguments are required and what kinds of inputs are valid is based on your properties' types.
44
+
-What kinds of inputs are valid, and whether arguments are required, is based on your properties' types and default values.
45
45
46
-
In this example, all of the properties have default values — Boolean flags always default to `false`, optional properties default to `nil`, and arrays default to an empty array. An option, flag, or argument with a `default` parameter can also be omitted by the user.
46
+
In this example, all of the properties have default values (optional properties default to `nil`).
47
47
48
48
Users must provide values for all properties with no implicit or specified default. For example, this command would require one integer argument and a string with the key `--user-name`.
49
49
@@ -70,12 +70,12 @@ Usage: example --user-name <user-name> <value>
70
70
See 'example --help' for more information.
71
71
```
72
72
73
-
When using the `default` parameter for an array property, the default values will not be included if additional values are passed on the command line.
73
+
When providing a default value for an array property, any user-supplied values replace the entire default.
74
74
75
-
```
75
+
```swift
76
76
structLucky: ParsableCommand {
77
-
@Argument(default: [7, 14, 21])
78
-
var numbers: [Int]
77
+
@Argument()
78
+
var numbers= [7, 14, 21]
79
79
80
80
mutatingfuncrun() throws {
81
81
print("""
@@ -104,10 +104,10 @@ You can override this default by specifying one or more name specifications in t
104
104
```swift
105
105
structExample: ParsableCommand {
106
106
@Flag(name: .long) // Same as the default
107
-
var stripWhitespace: Bool
107
+
var stripWhitespace=false
108
108
109
109
@Flag(name: .short)
110
-
var verbose: Bool
110
+
var verbose=false
111
111
112
112
@Option(name: .customLong("count"))
113
113
var iterationCount: Int
@@ -214,7 +214,7 @@ Flags are most frequently used for `Bool` properties. You can generate a `true`/
When providing a flag inversion, you can pass your own default with normal property initialization syntax (`@Flag var foo: Bool = true`). If you want to require that the user specify one of the two inversions, use a non-Optional type and do not pass a default value.
228
+
When declaring a flag with an inversion, set the default by specifying `true` or `false` as the property's initial value. If you want to require that the user specify one of the two inversions, leave off the default value.
229
229
230
230
In the `Example` command defined above, a flag is required for the `requiredElement` property. The specified prefixes are prepended to the long names for the flags:
231
231
@@ -241,19 +241,19 @@ Error: Missing one of: '--enable-required-element', '--disable-required-element'
241
241
To create a flag with custom names for a Boolean value, to provide an exclusive choice between more than two names, or for collecting multiple values from a set of defined choices, define an enumeration that conforms to the `EnumerableFlag` protocol.
242
242
243
243
```swift
244
-
enumCacheMethod: EnumerableFlag {
244
+
enumCacheMethod: String, EnumerableFlag {
245
245
caseinMemoryCache
246
246
casepersistentCache
247
247
}
248
248
249
-
enumColor: EnumerableFlag {
249
+
enumColor: String, EnumerableFlag {
250
250
casepink, purple, silver
251
251
}
252
252
253
253
structExample: ParsableCommand {
254
254
@Flag() var cacheMethod: CacheMethod
255
255
256
-
@Flag() var colors: [Color]
256
+
@Flag() var colors: [Color]= []
257
257
258
258
mutatingfuncrun() throws {
259
259
print(cacheMethod)
@@ -302,7 +302,7 @@ For example, this command defines a `--verbose` flag, a `--name` option, and an
302
302
303
303
```swift
304
304
structExample: ParsableCommand {
305
-
@Flag() var verbose: Bool
305
+
@Flag() var verbose=false
306
306
@Option() var name: String
307
307
@Argument() var file: String?
308
308
@@ -349,8 +349,8 @@ The default strategy for parsing options as arrays is to read each value from a
349
349
350
350
```swift
351
351
structExample: ParsableCommand {
352
-
@Option() var file: [String]
353
-
@Flag() var verbose: Bool
352
+
@Option() var file: [String]= []
353
+
@Flag() var verbose=false
354
354
355
355
mutatingfuncrun() throws {
356
356
print("Verbose: \(verbose), files: \(file)")
@@ -400,8 +400,8 @@ The default strategy for parsing arrays of positional arguments is to ignore al
0 commit comments