Skip to content

Commit e291285

Browse files
committed
Add UsageError.statusCode
1 parent b6bdb01 commit e291285

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### Changed
55
- Shell completion code is now printed by throwing a `PrintCompletionMessage` (a subclass of `PrintMessage`) rather than calling `echo` directly.
66

7+
### Added
8+
- `UsageError` now has a `statusCode` parameter (which defaults to 1). If you're using `ClicktCommand.main`, the value of `statusCode` will be passed to `exitProcess`.
9+
710
## [2.2.0] - 2019-09-25
811
### Added
912
- Added [`enum()` conversion](https://ajalt.github.io/clikt/api/clikt/com.github.ajalt.clikt.parameters.types/enum/) for options and arguments. ([#84](https://github.com/ajalt/clikt/issues/84))

clikt/src/main/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ abstract class CliktCommand(
240240
exitProcess(0)
241241
} catch (e: UsageError) {
242242
echo(e.helpMessage(), err = true)
243-
exitProcess(1)
243+
exitProcess(e.statusCode)
244244
} catch (e: CliktError) {
245245
echo(e.message, err = true)
246246
exitProcess(1)

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.ajalt.clikt.core
33
import com.github.ajalt.clikt.parameters.arguments.Argument
44
import com.github.ajalt.clikt.parameters.arguments.convert
55
import com.github.ajalt.clikt.parameters.options.Option
6+
import kotlin.system.exitProcess
67

78
/**
89
* An internal error that signals Clikt to abort.
@@ -54,21 +55,24 @@ class PrintCompletionMessage(message: String, val forceUnixLineEndings: Boolean)
5455
* actual name used. If not set, it will be inferred from [argument] or [option] if either is set.
5556
* @property option The option that caused this error. This may be set after the error is thrown.
5657
* @property argument The argument that caused this error. This may be set after the error is thrown.
58+
* @property statusCode The value to use as the exit code for the process. If you use
59+
* [CliktCommand.main], it will pass this value to [exitProcess] after printing [message]. Defaults to 1.
5760
*/
5861
open class UsageError private constructor(
5962
val text: String? = null,
6063
var paramName: String? = null,
6164
var option: Option? = null,
6265
var argument: Argument? = null,
63-
var context: Context? = null) : CliktError() {
64-
constructor(text: String, paramName: String? = null, context: Context? = null)
65-
: this(text, paramName, null, null, context)
66+
var context: Context? = null,
67+
val statusCode: Int = 1) : CliktError() {
68+
constructor(text: String, paramName: String? = null, context: Context? = null, statusCode: Int = 1)
69+
: this(text, paramName, null, null, context, statusCode)
6670

67-
constructor(text: String, argument: Argument, context: Context? = null)
68-
: this(text, null, null, argument, context)
71+
constructor(text: String, argument: Argument, context: Context? = null, statusCode: Int = 1)
72+
: this(text, null, null, argument, context, statusCode)
6973

70-
constructor(text: String, option: Option, context: Context? = null)
71-
: this(text, null, option, null, context)
74+
constructor(text: String, option: Option, context: Context? = null, statusCode: Int = 1)
75+
: this(text, null, option, null, context, statusCode)
7276

7377
fun helpMessage(): String = buildString {
7478
context?.let { append(it.command.getFormattedUsage()).append("\n\n") }

0 commit comments

Comments
 (0)