Skip to content

Provide Optional initial value for input property wrappers #556

Open
@tarbaiev-smg

Description

@tarbaiev-smg

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:

  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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions