Skip to content

Commit ca89041

Browse files
ibrahimoktayibrahim oktay
andauthored
Fix showing help message when “-h” is given as an input for an array type argument (#149) (#166)
When argument is an array and no input is given but “-h”, if you throw an Error in Validate function, it prints error and short usage instead of help message. This patch checks built-in flags before throwing a validation error. Solves #149 Co-authored-by: ibrahim oktay <[email protected]>
1 parent 85196ee commit ca89041

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Sources/ArgumentParser/Parsing/CommandParser.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extension CommandParser {
159159
do {
160160
try parsedCommand.validate()
161161
} catch {
162+
try checkForBuiltInFlags(split)
162163
throw CommandError(commandStack: commandStack, parserError: ParserError.userValidationError(error))
163164
}
164165

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,27 @@ extension HelpGenerationTests {
318318
""")
319319
}
320320

321+
struct K: ParsableCommand {
322+
@Argument(help: "A list of paths.")
323+
var paths: [String]
324+
325+
func validate() throws {
326+
if paths.isEmpty {
327+
throw ValidationError("At least one path must be specified.")
328+
}
329+
}
330+
}
331+
332+
func testHelpWithNoValueForArray() {
333+
AssertHelp(for: K.self, equals: """
334+
USAGE: k [<paths> ...]
335+
336+
ARGUMENTS:
337+
<paths> A list of paths.
338+
339+
OPTIONS:
340+
-h, --help Show help information.
341+
342+
""")
343+
}
321344
}

0 commit comments

Comments
 (0)