Skip to content

Commit 5ce40bb

Browse files
committed
WIP: add test coverage and fixes for #446
- 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.
1 parent 0672ff8 commit 5ce40bb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Sources/ArgumentParser/Parsable Properties/Argument.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ extension Argument {
244244
transform: @escaping (String) throws -> Value
245245
) {
246246
self.init(_parsedValue: .init { key in
247-
let help = ArgumentDefinition.Help(options: [], help: help, key: key)
247+
let help = ArgumentDefinition.Help(
248+
options: initial != nil ? [.isOptional] : [],
249+
help: help,
250+
defaultValue: initial.map { "\($0)" },
251+
key: key)
248252
let arg = ArgumentDefinition(kind: .positional, help: help, completion: completion ?? .default, update: .unary({
249253
(origin, name, valueString, parsedValues) in
250254
do {

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,4 +775,28 @@ extension HelpGenerationTests {
775775
See 'custom-usage-hidden --help' for more information.
776776
""")
777777
}
778+
779+
struct ArgumentsWithTransform: ParsableCommand {
780+
@Argument(help: "required", transform: { Int($0)! })
781+
var arg0: Int
782+
@Argument(help: "optional", transform: { Int($0)! })
783+
var arg1: Int = 0
784+
@Argument(help: "optional", transform: { Int($0)! })
785+
var arg2: Int?
786+
}
787+
788+
func testArgumentsWithTransform() {
789+
AssertHelp(.default, for: ArgumentsWithTransform.self, equals: """
790+
USAGE: arguments-with-transform <arg0> [<arg1>] [<arg2>]
791+
792+
ARGUMENTS:
793+
<arg0> required
794+
<arg1> optional (default: 0)
795+
<arg2> optional
796+
797+
OPTIONS:
798+
-h, --help Show help information.
799+
800+
""")
801+
}
778802
}

0 commit comments

Comments
 (0)