Skip to content

Commit

Permalink
Add error parameter to PrintHelp exceptions (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored Jun 1, 2020
1 parent 34ff1e8 commit 28e366e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
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

0 comments on commit 28e366e

Please sign in to comment.