Skip to content

Commit

Permalink
WIP: add test coverage and fixes for #466
Browse files Browse the repository at this point in the history
- Start of a fix for issue 466. After an initial read through the code
  it feels like there more @argument initializers than needed and this
  logic can probably be reudced and simplified.
- Fixes isOptional derivision for non-Optional @arguments with default
  values, however the simple solution doesn't work properly for Optional
  @arguments without a non-nil default value.
  • Loading branch information
rauhul committed Sep 9, 2022
1 parent 9f39744 commit 5a1e4b7
Show file tree
Hide file tree
Showing 16 changed files with 2,029 additions and 683 deletions.
622 changes: 376 additions & 246 deletions Sources/ArgumentParser/Parsable Properties/Argument.swift

Large diffs are not rendered by default.

32 changes: 29 additions & 3 deletions Sources/ArgumentParser/Parsable Properties/Flag.swift
Expand Up @@ -382,7 +382,14 @@ 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(
// TODO: (feedback from Nate) maybe this needs to be derived from allCases?
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 @@ -471,7 +478,17 @@ 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(
// TODO: (feedback from Nate) maybe this needs to be derived from allCases?
allValues: [],
options: [.isOptional],
help: helpForCase,
// TODO: (feedback from Nate) maybe this needs to be derived from allCases?
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 @@ -496,7 +513,16 @@ 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(
// TODO: (feedback from Nate) maybe this needs to be derived from allCases?
allValues: [],
options: [.isOptional],
help: helpForCase,
// TODO: (feedback from Nate) maybe this needs to be derived from allCases?
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 5a1e4b7

Please sign in to comment.