From edf96d712fd3d781134eabc7eba49729201a8058 Mon Sep 17 00:00:00 2001 From: ibrahim oktay Date: Sat, 23 May 2020 21:15:54 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20showing=20help=20message=20when=20?= =?UTF-8?q?=E2=80=9C-h=E2=80=9D=20is=20given=20as=20an=20input=20for=20an?= =?UTF-8?q?=20array=20type=20argument=20(#149)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../Parsing/CommandParser.swift | 1 + .../HelpGenerationTests.swift | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Sources/ArgumentParser/Parsing/CommandParser.swift b/Sources/ArgumentParser/Parsing/CommandParser.swift index d7642c89b..4ccf02b7e 100644 --- a/Sources/ArgumentParser/Parsing/CommandParser.swift +++ b/Sources/ArgumentParser/Parsing/CommandParser.swift @@ -159,6 +159,7 @@ extension CommandParser { do { try parsedCommand.validate() } catch { + try checkForBuiltInFlags(split) throw CommandError(commandStack: commandStack, parserError: ParserError.userValidationError(error)) } diff --git a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift index 122ddea04..be1e583bc 100644 --- a/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift +++ b/Tests/ArgumentParserUnitTests/HelpGenerationTests.swift @@ -317,4 +317,27 @@ extension HelpGenerationTests { """) } + struct K: ParsableCommand { + @Argument(help: "A list of paths.") + var paths: [String] + + func validate() throws { + if paths.isEmpty { + throw ValidationError("At least one path must be specified.") + } + } + } + + func testHelpWithNoValueForArray() { + AssertHelp(for: K.self, equals: """ + USAGE: k [ ...] + + ARGUMENTS: + A list of paths. + + OPTIONS: + -h, --help Show help information. + + """) + } }