Skip to content

Commit

Permalink
Unify @argument and @option initialization paths (#477)
Browse files Browse the repository at this point in the history
- Fixes #466.
- Adds initializers to ArgumentDefinition generic over a Container type.
  The Container type must conform to a new internal protocol
  ArgumentDefinitionContainer which describes functionality like default
  set of help options for the argument defined by the property wrapper,
  etc.
- Adds overloads for Optional @arguments and @options with default
  values which emit deprecation warning to guide users towards using the
  non-Optional versions.
  • Loading branch information
rauhul committed Sep 10, 2022
1 parent 09106ba commit 607021b
Show file tree
Hide file tree
Showing 16 changed files with 1,937 additions and 705 deletions.
584 changes: 326 additions & 258 deletions Sources/ArgumentParser/Parsable Properties/Argument.swift

Large diffs are not rendered by default.

27 changes: 24 additions & 3 deletions Sources/ArgumentParser/Parsable Properties/Flag.swift
Expand Up @@ -400,7 +400,13 @@ extension Flag where Value: EnumerableFlag {
let caseKey = InputKey(rawValue: String(describing: value))
let name = Value.name(for: value)
let helpForCase = hasCustomCaseHelp ? (caseHelps[i] ?? help) : help
let help = ArgumentDefinition.Help(options: initial != nil ? .isOptional : [], help: helpForCase, defaultValue: defaultValue, key: key, isComposite: !hasCustomCaseHelp)
let help = ArgumentDefinition.Help(
allValues: [],
options: initial != nil ? [.isOptional] : [],
help: helpForCase,
defaultValue: defaultValue,
key: key,
isComposite: !hasCustomCaseHelp)
return ArgumentDefinition.flag(name: name, key: key, caseKey: caseKey, help: help, parsingStrategy: .default, initialValue: initial, update: .nullary({ (origin, name, values) in
hasUpdated = try ArgumentSet.updateFlag(key: key, value: value, origin: origin, values: &values, hasUpdated: hasUpdated, exclusivity: exclusivity)
}))
Expand Down Expand Up @@ -489,7 +495,15 @@ extension Flag {
let caseKey = InputKey(rawValue: String(describing: value))
let name = Element.name(for: value)
let helpForCase = hasCustomCaseHelp ? (caseHelps[i] ?? help) : help
let help = ArgumentDefinition.Help(options: .isOptional, help: helpForCase, key: key, isComposite: !hasCustomCaseHelp)

let help = ArgumentDefinition.Help(
allValues: [],
options: [.isOptional],
help: helpForCase,
defaultValue: nil,
key: key,
isComposite: !hasCustomCaseHelp)

return ArgumentDefinition.flag(name: name, key: key, caseKey: caseKey, help: help, parsingStrategy: .default, initialValue: nil as Element?, update: .nullary({ (origin, name, values) in
hasUpdated = try ArgumentSet.updateFlag(key: key, value: value, origin: origin, values: &values, hasUpdated: hasUpdated, exclusivity: exclusivity)
}))
Expand All @@ -514,7 +528,14 @@ extension Flag {
let caseKey = InputKey(rawValue: String(describing: value))
let name = Element.name(for: value)
let helpForCase = hasCustomCaseHelp ? (caseHelps[i] ?? help) : help
let help = ArgumentDefinition.Help(options: .isOptional, help: helpForCase, key: key, isComposite: !hasCustomCaseHelp)
let help = ArgumentDefinition.Help(
allValues: [],
options: [.isOptional],
help: helpForCase,
defaultValue: nil,
key: key,
isComposite: !hasCustomCaseHelp)

return ArgumentDefinition.flag(name: name, key: key, caseKey: caseKey, help: help, parsingStrategy: .default, initialValue: initial, update: .nullary({ (origin, name, values) in
values.update(forKey: key, inputOrigin: origin, initial: [Element](), closure: {
$0.append(value)
Expand Down

0 comments on commit 607021b

Please sign in to comment.