Skip to content

Commit 8cf21ad

Browse files
authored
Change statusCode for PrintHelpMessage to 0 (#425)
1 parent b0a12bc commit 8cf21ad

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
### Changed
99
- Updated Kotlin to 1.9.0
10-
- `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))
10+
- `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))
1111

1212
## 4.0.0
1313
### Added

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/exceptions.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ class PrintHelpMessage(
4848
* If true, the error message should be printed to stderr.
4949
*/
5050
val error: Boolean = false,
51-
) : CliktError(printError = false), ContextCliktError
51+
/**
52+
* The value to use as the exit code for the process.
53+
*
54+
* If you use [CliktCommand.main], it will pass this value to `exitProcess` after printing
55+
* [message]. Defaults to 0.
56+
*/
57+
statusCode: Int = 0,
58+
) : CliktError(printError = false, statusCode = statusCode), ContextCliktError
5259

5360
/**
5461
* An exception that indicates that a message should be printed.

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/completion/CompletionTestBase.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import kotlin.test.assertEquals
1919
abstract class CompletionTestBase(private val shell: String) {
2020

2121
private fun doTest(expected: String, command: TestCommand) {
22-
val message = shouldThrow<PrintCompletionMessage> {
22+
val exception = shouldThrow<PrintCompletionMessage> {
2323
command.parse("--generate-completion=$shell")
24-
}.message
25-
assertEquals(expected.trimMargin(), message)
24+
}
25+
assertEquals(expected.trimMargin(), exception.message)
26+
assertEquals(exception.statusCode, 0)
2627
}
2728

2829
@JsName("custom_completions_expected")

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/core/ContextTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ class ContextTest {
9191
fun `default help option names`() {
9292
class C : TestCommand()
9393

94-
shouldThrow<PrintHelpMessage> { C().parse("--help") }
94+
shouldThrow<PrintHelpMessage> {
95+
C().parse("--help")
96+
}.statusCode shouldBe 0
9597
shouldThrow<PrintHelpMessage> { C().parse("-h") }
9698
shouldThrow<PrintHelpMessage> {
9799
C().context { helpOptionNames = setOf("-x") }.parse("-x")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.ajalt.clikt.core
2+
3+
import com.github.ajalt.clikt.testing.TestCommand
4+
import com.github.ajalt.clikt.testing.test
5+
import io.kotest.data.blocking.forAll
6+
import io.kotest.data.row
7+
import io.kotest.matchers.shouldBe
8+
import kotlin.js.JsName
9+
import kotlin.test.Test
10+
11+
class ExceptionsTest {
12+
@Test
13+
@JsName("exceptions_statusCode")
14+
fun `exceptions statusCode`() = forAll(
15+
row(CliktError(), 1),
16+
row(CliktError(statusCode = 2), 2),
17+
row(UsageError(""), 1),
18+
row(UsageError("", statusCode = 2), 2),
19+
row(PrintHelpMessage(null), 0),
20+
row(PrintHelpMessage(null, statusCode = 2), 2),
21+
row(PrintMessage(""), 0),
22+
row(PrintMessage("", statusCode = 2), 2),
23+
row(PrintCompletionMessage(""), 0),
24+
row(Abort(), 1),
25+
row(ProgramResult(2), 2),
26+
row(NoSuchOption(""), 1),
27+
) { err, code ->
28+
class C : TestCommand() {
29+
override fun run_() {
30+
throw err
31+
}
32+
}
33+
C().test("").statusCode shouldBe code
34+
}
35+
}

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/parameters/EagerOptionsTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ class EagerOptionsTest {
124124
}
125125
}
126126

127-
shouldThrow<PrintMessage> {
127+
val exception = shouldThrow<PrintMessage> {
128128
C().parse("--version")
129-
}.formattedMessage shouldBe "prog version 1.2.3"
129+
}
130+
exception.formattedMessage shouldBe "prog version 1.2.3"
131+
exception.statusCode shouldBe 0
130132
}
131133

132134
@Test

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/parameters/OptionTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ class OptionTest {
4141
val fob by option(hidden = true)
4242
}
4343

44-
shouldThrow<NoSuchOption> {
44+
val exception = shouldThrow<NoSuchOption> {
4545
C().parse(argv)
46-
}.formattedMessage shouldBe message
46+
}
47+
exception.formattedMessage shouldBe message
48+
exception.statusCode shouldBe 1
4749
}
4850

4951
@Test

0 commit comments

Comments
 (0)