Skip to content

Commit

Permalink
Change statusCode for PrintHelpMessage to 0 (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored Jul 11, 2023
1 parent b0a12bc commit 8cf21ad
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

### Changed
- Updated Kotlin to 1.9.0
- `PrintMessage` and `PrintCompletionMessage` now default to exiting with a status code 0, which is the behavior they had in 3.x. ([#419](https://github.com/ajalt/clikt/issues/419))
- `PrintMessage`, `PrintHelpMessage` and `PrintCompletionMessage` now default to exiting with a status code 0, which is the behavior they had in 3.x. ([#419](https://github.com/ajalt/clikt/issues/419))

## 4.0.0
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ class PrintHelpMessage(
* If true, the error message should be printed to stderr.
*/
val error: Boolean = false,
) : CliktError(printError = false), ContextCliktError
/**
* The value to use as the exit code for the process.
*
* If you use [CliktCommand.main], it will pass this value to `exitProcess` after printing
* [message]. Defaults to 0.
*/
statusCode: Int = 0,
) : CliktError(printError = false, statusCode = statusCode), ContextCliktError

/**
* An exception that indicates that a message should be printed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import kotlin.test.assertEquals
abstract class CompletionTestBase(private val shell: String) {

private fun doTest(expected: String, command: TestCommand) {
val message = shouldThrow<PrintCompletionMessage> {
val exception = shouldThrow<PrintCompletionMessage> {
command.parse("--generate-completion=$shell")
}.message
assertEquals(expected.trimMargin(), message)
}
assertEquals(expected.trimMargin(), exception.message)
assertEquals(exception.statusCode, 0)
}

@JsName("custom_completions_expected")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class ContextTest {
fun `default help option names`() {
class C : TestCommand()

shouldThrow<PrintHelpMessage> { C().parse("--help") }
shouldThrow<PrintHelpMessage> {
C().parse("--help")
}.statusCode shouldBe 0
shouldThrow<PrintHelpMessage> { C().parse("-h") }
shouldThrow<PrintHelpMessage> {
C().context { helpOptionNames = setOf("-x") }.parse("-x")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.ajalt.clikt.core

import com.github.ajalt.clikt.testing.TestCommand
import com.github.ajalt.clikt.testing.test
import io.kotest.data.blocking.forAll
import io.kotest.data.row
import io.kotest.matchers.shouldBe
import kotlin.js.JsName
import kotlin.test.Test

class ExceptionsTest {
@Test
@JsName("exceptions_statusCode")
fun `exceptions statusCode`() = forAll(
row(CliktError(), 1),
row(CliktError(statusCode = 2), 2),
row(UsageError(""), 1),
row(UsageError("", statusCode = 2), 2),
row(PrintHelpMessage(null), 0),
row(PrintHelpMessage(null, statusCode = 2), 2),
row(PrintMessage(""), 0),
row(PrintMessage("", statusCode = 2), 2),
row(PrintCompletionMessage(""), 0),
row(Abort(), 1),
row(ProgramResult(2), 2),
row(NoSuchOption(""), 1),
) { err, code ->
class C : TestCommand() {
override fun run_() {
throw err
}
}
C().test("").statusCode shouldBe code
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ class EagerOptionsTest {
}
}

shouldThrow<PrintMessage> {
val exception = shouldThrow<PrintMessage> {
C().parse("--version")
}.formattedMessage shouldBe "prog version 1.2.3"
}
exception.formattedMessage shouldBe "prog version 1.2.3"
exception.statusCode shouldBe 0
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class OptionTest {
val fob by option(hidden = true)
}

shouldThrow<NoSuchOption> {
val exception = shouldThrow<NoSuchOption> {
C().parse(argv)
}.formattedMessage shouldBe message
}
exception.formattedMessage shouldBe message
exception.statusCode shouldBe 1
}

@Test
Expand Down

0 comments on commit 8cf21ad

Please sign in to comment.