From 28e366eb403a1959408124dd963eec91ef24c0e8 Mon Sep 17 00:00:00 2001 From: AJ Alt Date: Mon, 1 Jun 2020 10:12:28 -0700 Subject: [PATCH] Add error parameter to PrintHelp exceptions (#188) --- CHANGELOG.md | 5 +++++ .../com/github/ajalt/clikt/core/CliktCommand.kt | 4 ++-- .../kotlin/com/github/ajalt/clikt/core/exceptions.kt | 12 ++++++++---- .../kotlin/com/github/ajalt/clikt/parsers/Parser.kt | 4 ++-- .../com/github/ajalt/clikt/core/CliktCommandTest.kt | 6 ++++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08471c0b2..ec9b58b87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog ## [Unreleased] +### Added +- Added `error` parameter to `PrintMessage` and `PrintHelpMessage`. When `true`, `CliktCommand.main` will exit with status code 1. ([#187](https://github.com/ajalt/clikt/issues/187)) + +### Changed +- When `printHelpOnEmptyArgs` is `true` and no arguments are present, or when `invokeWithoutSubcommand` is `false` and no subcommand is present, `CliktCommand.main` will now exit with status code 1 rather than 0. ## [2.7.1] - 2020-05-19 ### Fixed diff --git a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt index c91cc025b..c5eb4f5b2 100644 --- a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt +++ b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt @@ -275,14 +275,14 @@ abstract class CliktCommand( exitProcessMpp(e.statusCode) } catch (e: PrintHelpMessage) { echo(e.command.getFormattedHelp()) - exitProcessMpp(0) + exitProcessMpp(if (e.error) 1 else 0) } catch (e: PrintCompletionMessage) { val s = if (e.forceUnixLineEndings) "\n" else currentContext.console.lineSeparator echo(e.message, lineSeparator = s) exitProcessMpp(0) } catch (e: PrintMessage) { echo(e.message) - exitProcessMpp(0) + exitProcessMpp(if (e.error) 1 else 0) } catch (e: UsageError) { echo(e.helpMessage(), err = true) exitProcessMpp(e.statusCode) diff --git a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/exceptions.kt b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/exceptions.kt index 5e63158dc..118739ac8 100644 --- a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/exceptions.kt +++ b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/exceptions.kt @@ -22,16 +22,20 @@ open class CliktError(message: String? = null, cause: Exception? = null) : Runti /** * An exception that indicates that the command's help should be printed. * - * Execution should be immediately halted without an error. + * Execution should be immediately halted. + * + * @property error If true, execution should halt with an error. Otherwise, execution halt with no error code. */ -class PrintHelpMessage(val command: CliktCommand) : CliktError() +class PrintHelpMessage(val command: CliktCommand, val error: Boolean = false) : CliktError() /** * An exception that indicates that a message should be printed. * - * Execution should be immediately halted without an error. + * Execution should be immediately halted. + * + * @property error If true, execution should halt with an error. Otherwise, execution halt with no error code. */ -open class PrintMessage(message: String) : CliktError(message) +open class PrintMessage(message: String, val error: Boolean = false) : CliktError(message) /** * Indicate that that the program finished in a controlled manner, and should complete with the given [statusCode] diff --git a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parsers/Parser.kt b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parsers/Parser.kt index e57631653..3695a4cba 100644 --- a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parsers/Parser.kt +++ b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parsers/Parser.kt @@ -37,7 +37,7 @@ internal object Parser { prefixes.remove("") if (startingArgI > tokens.lastIndex && command.printHelpOnEmptyArgs) { - throw PrintHelpMessage(command) + throw PrintHelpMessage(command, error = true) } val positionalArgs = ArrayList() @@ -153,7 +153,7 @@ internal object Parser { command._arguments.forEach { it.postValidate(context) } if (subcommand == null && subcommands.isNotEmpty() && !command.invokeWithoutSubcommand) { - throw PrintHelpMessage(command) + throw PrintHelpMessage(command, error = true) } command.currentContext.invokedSubcommand = subcommand diff --git a/clikt/src/commonTest/kotlin/com/github/ajalt/clikt/core/CliktCommandTest.kt b/clikt/src/commonTest/kotlin/com/github/ajalt/clikt/core/CliktCommandTest.kt index 3974decda..21152895c 100644 --- a/clikt/src/commonTest/kotlin/com/github/ajalt/clikt/core/CliktCommandTest.kt +++ b/clikt/src/commonTest/kotlin/com/github/ajalt/clikt/core/CliktCommandTest.kt @@ -42,7 +42,7 @@ class CliktCommandTest { fun `invokeWithoutSubcommand=false`() { shouldThrow { TestCommand(called = false).subcommands(TestCommand(called = false)).parse("") - } + }.error shouldBe true val child = TestCommand(called = true, name = "foo") TestCommand(called = true).subcommands(child).apply { @@ -73,7 +73,9 @@ class CliktCommandTest { @JsName("printHelpOnEmptyArgs__true") fun `printHelpOnEmptyArgs = true`() { class C : TestCommand(called = false, printHelpOnEmptyArgs = true) - shouldThrow { C().parse("") } + shouldThrow { + C().parse("") + }.error shouldBe true } @Test