Skip to content

Commit

Permalink
Add UsageError.statusCode
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Oct 31, 2019
1 parent b6bdb01 commit e291285
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### Changed
- Shell completion code is now printed by throwing a `PrintCompletionMessage` (a subclass of `PrintMessage`) rather than calling `echo` directly.

### Added
- `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`.

## [2.2.0] - 2019-09-25
### Added
- 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ abstract class CliktCommand(
exitProcess(0)
} catch (e: UsageError) {
echo(e.helpMessage(), err = true)
exitProcess(1)
exitProcess(e.statusCode)
} catch (e: CliktError) {
echo(e.message, err = true)
exitProcess(1)
Expand Down
18 changes: 11 additions & 7 deletions clikt/src/main/kotlin/com/github/ajalt/clikt/core/exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ajalt.clikt.core
import com.github.ajalt.clikt.parameters.arguments.Argument
import com.github.ajalt.clikt.parameters.arguments.convert
import com.github.ajalt.clikt.parameters.options.Option
import kotlin.system.exitProcess

/**
* An internal error that signals Clikt to abort.
Expand Down Expand Up @@ -54,21 +55,24 @@ class PrintCompletionMessage(message: String, val forceUnixLineEndings: Boolean)
* actual name used. If not set, it will be inferred from [argument] or [option] if either is set.
* @property option The option that caused this error. This may be set after the error is thrown.
* @property argument The argument that caused this error. This may be set after the error is thrown.
* @property statusCode 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 1.
*/
open class UsageError private constructor(
val text: String? = null,
var paramName: String? = null,
var option: Option? = null,
var argument: Argument? = null,
var context: Context? = null) : CliktError() {
constructor(text: String, paramName: String? = null, context: Context? = null)
: this(text, paramName, null, null, context)
var context: Context? = null,
val statusCode: Int = 1) : CliktError() {
constructor(text: String, paramName: String? = null, context: Context? = null, statusCode: Int = 1)
: this(text, paramName, null, null, context, statusCode)

constructor(text: String, argument: Argument, context: Context? = null)
: this(text, null, null, argument, context)
constructor(text: String, argument: Argument, context: Context? = null, statusCode: Int = 1)
: this(text, null, null, argument, context, statusCode)

constructor(text: String, option: Option, context: Context? = null)
: this(text, null, option, null, context)
constructor(text: String, option: Option, context: Context? = null, statusCode: Int = 1)
: this(text, null, option, null, context, statusCode)

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

0 comments on commit e291285

Please sign in to comment.