Skip to content

Commit

Permalink
Don't report an error for grouped short options with treatUnknownOpti…
Browse files Browse the repository at this point in the history
…onsAsArgs
  • Loading branch information
ajalt committed Jun 16, 2023
1 parent 7cd3753 commit 9c24b9e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

### Fixed
- When parsing a command line with more than one error, Clikt will now always report the error that occurs earliest ([#361](https://github.com/ajalt/clikt/issues/361))
- When `treatUnknownOptionsAsArgs` is true, grouped unknown short options will now be treated as arguments rather than reporting an error.

### Removed
- Removed `CliktConsole`. Mordant is now used for all input and output. If you were defining a custom console, instead define a mordant `TerminalInterface` and set it on your context's `Terminal`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ internal object Parser {
if (i == 0) continue // skip the dash

val name = context.tokenTransformer(context, prefix + opt)
val option = optionsByName[name] ?: if (ignoreUnknown && i == 1) {
val option = optionsByName[name] ?: if (ignoreUnknown) {
return OptParseResult(1, unknown = listOf(tok))
} else {
val possibilities = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,25 @@ class CliktCommandTest {
@Test
@JsName("treat_unknown_options_as_arguments_with_grouped_flag")
fun `treat unknown options as arguments with grouped flag`() {
class C(called: Boolean) : TestCommand(called = called, treatUnknownOptionsAsArgs = true) {
class C : TestCommand(treatUnknownOptionsAsArgs = true) {
val foo by option("-f").flag()
val args by argument().multiple()
}

with(C(true)) {
with(C()) {
parse("-f -g -i")
foo shouldBe true
args shouldBe listOf("-g", "-i")
}
with(C(true)) {
with(C()) {
parse("-f -gi")
foo shouldBe true
args shouldBe listOf("-gi")
}
shouldThrow<NoSuchOption> {
C(false).parse("-fgi")
}.formattedMessage shouldBe "no such option: -g"
with(C()) {
parse("-fgi")
foo shouldBe false
args shouldBe listOf("-fgi")
}
}
}

0 comments on commit 9c24b9e

Please sign in to comment.