apple / apple/swift-argument-parser

Provide Optional `initial` value for input property wrappers

Open
#556 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.8k
Forks
411
Avg merge
7d 14h
Merged PRs (30d)
15

Description

Currently it's only possible to use initial value if the property type is non-`Optional`.
I'd like to use environment variable as a fallback value for a command line argument (similar how it's done in Fastlane), but keep the argument required. To allow this I'd like to propose adding the `initial` argument to initializers of non-`Optional` type property wrappers which do not contain the `wrappedValue` argument.

For example adjusting the `Option` initializer:
```swift
public init(
name: NameSpecification = .long,
parsing parsingStrategy: SingleValueParsingStrategy = .next,
help: ArgumentHelp? = nil,
completion: CompletionKind? = nil,
initial: Value? = nil // <-- Using default value preserves backward compatibility
) {
self.init(_parsedValue: .init { key in
let arg = ArgumentDefinition(
container: Bare.self,
key: key,
kind: .name(key: key, specification: name),
help: help,
parsingStrategy: parsingStrategy.base,
initial: initial, // <-- injecting the initial value
completion: completion)

return ArgumentSet(arg)
})
```

would allow us to use it as follows:

```swift
@Option(initial: ProcessInfo.processInfo["REQUIRED_OPTION"])
var requiredOption: String
```

Contributor guide

Open the contributing guide

Research direction

Start at the Option initializer shown in the issue and trace how it constructs ArgumentDefinition and passes the initial value. Check the other non-Optional property-wrapper initializers that lack wrappedValue. Done means those wrappers accept an optional initial argument without breaking existing calls and the required-option environment-variable example works.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.