Skip to content

Commit

Permalink
WIP: add test coverage and fixes for #446
Browse files Browse the repository at this point in the history
- Start of a fix for issue 446. 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 Aug 26, 2022
1 parent 0672ff8 commit 5ce40bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/ArgumentParser/Parsable Properties/Argument.swift
Expand Up @@ -244,7 +244,11 @@ extension Argument {
transform: @escaping (String) throws -> Value
) {
self.init(_parsedValue: .init { key in
let help = ArgumentDefinition.Help(options: [], help: help, key: key)
let help = ArgumentDefinition.Help(
options: initial != nil ? [.isOptional] : [],
help: help,
defaultValue: initial.map { "\($0)" },
key: key)
let arg = ArgumentDefinition(kind: .positional, help: help, completion: completion ?? .default, update: .unary({
(origin, name, valueString, parsedValues) in
do {
Expand Down
24 changes: 24 additions & 0 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Expand Up @@ -775,4 +775,28 @@ extension HelpGenerationTests {
See 'custom-usage-hidden --help' for more information.
""")
}

struct ArgumentsWithTransform: ParsableCommand {
@Argument(help: "required", transform: { Int($0)! })
var arg0: Int
@Argument(help: "optional", transform: { Int($0)! })
var arg1: Int = 0
@Argument(help: "optional", transform: { Int($0)! })
var arg2: Int?
}

func testArgumentsWithTransform() {
AssertHelp(.default, for: ArgumentsWithTransform.self, equals: """
USAGE: arguments-with-transform <arg0> [<arg1>] [<arg2>]
ARGUMENTS:
<arg0> required
<arg1> optional (default: 0)
<arg2> optional
OPTIONS:
-h, --help Show help information.
""")
}
}

0 comments on commit 5ce40bb

Please sign in to comment.