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
Allow required array arguments, options, and flags (#196)
* Allow required arrays in `Option`s
Un-deprecates (but changes the semantics of) an initializer for an array value type without a default, forcing the user to specify at least one value from the command line.
* Allow required arrays in `Argument`s
Extends the parent commit to arguments, still un-deprecating and changing the semantics of the previous initializer to force users to provide a value on the command line.
* Allow required arrays in `Flag`s
Extends the previous commits to flags, still un-deprecating and changing the semantics of the previous initializer to force users to provide a value on the command line.
* Add default-value section to documentation
Copy file name to clipboardExpand all lines: Documentation/02 Arguments, Options, and Flags.md
+40Lines changed: 40 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -285,6 +285,46 @@ Verbosity level: 1
285
285
Verbosity level: 4
286
286
```
287
287
288
+
289
+
## Specifying default values
290
+
291
+
You can specify default values for almost all supported argument, option, and flag types using normal property initialization syntax:
292
+
293
+
```swift
294
+
enumCustomFlag: String, EnumerableFlag {
295
+
casefoo, bar, baz
296
+
}
297
+
298
+
structExample: ParsableCommand {
299
+
@Flag
300
+
var booleanFlag =false
301
+
302
+
@Flag
303
+
var arrayFlag: [CustomFlag] = [.foo, .baz]
304
+
305
+
@Option
306
+
var singleOption =0
307
+
308
+
@Option
309
+
var arrayOption = ["bar", "qux"]
310
+
311
+
@Argument
312
+
var singleArgument ="quux"
313
+
314
+
@Argument
315
+
var arrayArgument = ["quux", "quuz"]
316
+
}
317
+
```
318
+
319
+
This includes all of the variants of the argument types above (including `@Option(transform: ...)`, etc.), with a few notable exceptions:
320
+
-`Optional`-typed values (which default to `nil` and for which a default would not make sense, as the value could never be `nil`)
321
+
-`Int` flags (which are used for counting the number of times a flag is specified and therefore default to `0`)
322
+
323
+
If a default is not specified, the user must provide a value for that argument/option/flag or will receive an error that the value is missing.
324
+
325
+
You must also always specify a default of `false` for a non-optional `Bool` flag, as in the example above. This makes the behavior consistent with both normal Swift properties (which either must be explicitly initialized or optional to initialize a `struct`/`class` containing them) and the other property types.
326
+
327
+
288
328
## Specifying a parsing strategy
289
329
290
330
When parsing a list of command-line inputs, `ArgumentParser` distinguishes between dash-prefixed keys and un-prefixed values. When looking for the value for a key, only an un-prefixed value will be selected by default.
/// This private `init` allows us to expose multiple other similar constructors to allow for standard default property initialization while reducing code duplication.
375
+
privateinit<Element>(
376
+
initial:Value?,
377
+
parsingStrategy:ArgumentArrayParsingStrategy,
378
+
help:ArgumentHelp?,
379
+
completion:CompletionKind?
383
380
)
384
381
where Element:ExpressibleByArgument, Value ==Array<Element>
385
382
{
386
383
self.init(_parsedValue:.init { key in
384
+
// Assign the initial-value setter and help text for default value based on if an initial value was provided.
where Element:ExpressibleByArgument, Value ==Array<Element>
450
+
{
451
+
self.init(
452
+
initial:nil,
453
+
parsingStrategy: parsingStrategy,
454
+
help: help,
455
+
completion: completion
456
+
)
457
+
}
458
+
459
+
/// Creates an array property with an optional default value, intended to be called by other constructors to centralize logic.
460
+
///
461
+
/// This private `init` allows us to expose multiple other similar constructors to allow for standard default property initialization while reducing code duplication.
462
+
privateinit<Element>(
463
+
initial:Value?,
464
+
parsingStrategy:ArgumentArrayParsingStrategy,
465
+
help:ArgumentHelp?,
466
+
completion:CompletionKind?,
417
467
transform:@escaping(String)throws->Element
418
468
)
419
469
where Value ==Array<Element>
420
470
{
421
471
self.init(_parsedValue:.init { key in
472
+
// Assign the initial-value setter and help text for default value based on if an initial value was provided.
Copy file name to clipboardExpand all lines: Sources/ArgumentParser/Parsable Properties/Flag.swift
+40-15Lines changed: 40 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -530,18 +530,12 @@ extension Flag {
530
530
:ArgumentSet(additive: args)
531
531
})
532
532
}
533
-
534
-
/// Creates an array property that gets its values from the presence of
535
-
/// zero or more flags, where the allowed flags are defined by an
536
-
/// `EnumerableFlag` type.
537
-
///
538
-
/// This property has an empty array as its default value.
533
+
534
+
/// Creates an array property with an optional default value, intended to be called by other constructors to centralize logic.
539
535
///
540
-
/// - Parameters:
541
-
/// - name: A specification for what names are allowed for this flag.
542
-
/// - help: Information about how to use this flag.
543
-
publicinit<Element>(
544
-
wrappedValue:[Element],
536
+
/// This private `init` allows us to expose multiple other similar constructors to allow for standard default property initialization while reducing code duplication.
537
+
privateinit<Element>(
538
+
initial:[Element]?,
545
539
help:ArgumentHelp?=nil
546
540
)where Value ==Array<Element>, Element:EnumerableFlag{
547
541
self.init(_parsedValue:.init { key in
@@ -553,7 +547,7 @@ extension Flag {
553
547
letname=Element.name(for: value)
554
548
lethelpForCase= hasCustomCaseHelp ?(caseHelps[i]?? help): help
@available(*, deprecated, message:"Provide an empty array literal as a default value.")
559
+
560
+
/// Creates an array property that gets its values from the presence of
561
+
/// zero or more flags, where the allowed flags are defined by an
562
+
/// `EnumerableFlag` type.
563
+
///
564
+
/// This property has an empty array as its default value.
565
+
///
566
+
/// - Parameters:
567
+
/// - name: A specification for what names are allowed for this flag.
568
+
/// - help: Information about how to use this flag.
569
+
publicinit<Element>(
570
+
wrappedValue:[Element],
571
+
help:ArgumentHelp?=nil
572
+
)where Value ==Array<Element>, Element:EnumerableFlag{
573
+
self.init(
574
+
initial: wrappedValue,
575
+
help: help
576
+
)
577
+
}
578
+
579
+
/// Creates an array property with no default value that gets its values from the presence of zero or more flags, where the allowed flags are defined by an `EnumerableFlag` type.
580
+
///
581
+
/// This method is called to initialize an array `Flag` with no default value such as:
582
+
/// ```swift
583
+
/// @Flag
584
+
/// var foo: [CustomFlagType]
585
+
/// ```
586
+
///
587
+
/// - Parameters:
588
+
/// - help: Information about how to use this flag.
567
589
publicinit<Element>(
568
590
help:ArgumentHelp?=nil
569
591
)where Value ==Array<Element>, Element:EnumerableFlag{
0 commit comments