Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provides initial initializer parameter for input property wrappers #557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tarbaiev-smg
Copy link

@tarbaiev-smg tarbaiev-smg commented Feb 27, 2023

Description

Currently it's only possible to use initial value if the property type has a non-Optional type (by means of the wrappedValue parameter).
In some cases we might need to provide an optional default value, e.g when reading it from the environment.
To allow this we introduce the initial argument in the initializers of the Argument and Option property wrappers which do not contain the wrappedValue argument.

Resolves #556

Detailed Design

As an example, adding the initial parameter to the Option initializer:

  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<Value>.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:

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

@Option(initial: ProcessInfo.processInfo["OPTIONAL_OPTION"])
var optionalOption: String?

The argument requirement therefore remains defined by it's nullability. A nil initial value in a non-Optional property would fail the command if the argument is not provided in the command line.

Documentation Plan

The initialzers' documentation has been updated.

Test Plan

Relevant tests added.

Source Impact

The change is backward-compatible.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

…property wrappers

This allows using `Optional` initial values in addition to non-`Optional`, e.g. environment variables.
@tarbaiev-smg tarbaiev-smg force-pushed the optional-initial-value-initializer-parameter branch from ff43a17 to 14cb099 Compare February 27, 2023 14:00
@Coeur
Copy link
Contributor

Coeur commented Mar 6, 2024

@tarbaiev-smg I believe you'd need to rebase your PR, since it now has conflicts that must be resolved.

@tarbayev
Copy link

tarbayev commented Mar 6, 2024

@Coeur I'd be happy to, but first I'd like to get a confirmation that it still has a chance to be merged. It's been a year since I opened it.

@Coeur
Copy link
Contributor

Coeur commented Mar 8, 2024

@tarbayev I don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide Optional initial value for input property wrappers
3 participants