Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error parameter to PrintHelp exceptions #188

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CliktCommandTest {
fun `invokeWithoutSubcommand=false`() {
shouldThrow<PrintHelpMessage> {
TestCommand(called = false).subcommands(TestCommand(called = false)).parse("")
}
}.error shouldBe true

val child = TestCommand(called = true, name = "foo")
TestCommand(called = true).subcommands(child).apply {
Expand Down Expand Up @@ -73,7 +73,9 @@ class CliktCommandTest {
@JsName("printHelpOnEmptyArgs__true")
fun `printHelpOnEmptyArgs = true`() {
class C : TestCommand(called = false, printHelpOnEmptyArgs = true)
shouldThrow<PrintHelpMessage> { C().parse("") }
shouldThrow<PrintHelpMessage> {
C().parse("")
}.error shouldBe true
}

@Test
Expand Down