Skip to content

Commit

Permalink
Support aliasing options to other options (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored Aug 8, 2024
1 parent 5c56161 commit 9f48ab1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
- Added `Context.exitProcess` which you can use to prevent the process from exiting during tests.
- Added core module that supports watchOS, tvOS, and wasmWasi targets and has no dependencies.
- Added more options to `CliktCommand.test` to control the terminal interactivity. ([#517](https://github.com/ajalt/clikt/pull/517))
- Added `associate{}`, `associateBy{}`, and `associateWith{}' transforms for options that allow you to convert the keys and values of the map. ([#529](https://github.com/ajalt/clikt/pull/529))
- Added `associate{}`, `associateBy{}`, and `associateWith{}` transforms for options that allow you to convert the keys and values of the map. ([#529](https://github.com/ajalt/clikt/pull/529))
- Added support for aliasing options to other options. ([#535](https://github.com/ajalt/clikt/pull/535))

### Changed
- In a subcommand with `argument().multiple()`, the behavior is now the same regardless of the value of `allowMultipleSubcommands`: if a token matches a subcommand name, it's now treated as a subcommand rather than a positional argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ class CliktCommandTest {

@Test
fun aliases() = forAll(
row("-xx", "x", emptyList()),
row("a", "a", listOf("b")),
row("y x", "x", emptyList()),
row("a", "a", listOf("b")),
row("b", null, listOf("-xa")),
row("--alias", "c", emptyList()),
row("--", "d", emptyList()),
row("@f", "e", emptyList()),
row("recurse", null, listOf("recurse")),
row("recurse2", "foo", listOf("recurse", "recurse2"))
) { argv, ex, ey ->
Expand All @@ -152,7 +154,10 @@ class CliktCommandTest {
"a" to listOf("-xa", "b"),
"b" to listOf("--", "-xa"),
"recurse" to listOf("recurse"),
"recurse2" to listOf("recurse", "--xx=foo", "recurse2")
"recurse2" to listOf("recurse", "--xx=foo", "recurse2"),
"--alias" to listOf("--xx=c"),
"--" to listOf("-xd"),
"@f" to listOf("-xe"),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,8 @@ class OptionTest {
}

@Test
@JsName("aliased_tokens")
fun `aliased tokens`() = forAll(
@JsName("token_transform_alias")
fun `token transform alias`() = forAll(
row("", null),
row("--yy 3", "3")
) { argv, expected ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ private class CommandParser<T : BaseCliktCommand<T>>(
val normTok = context.transformToken(context, tok)
val prefix = splitOptionPrefix(tok).first
when {
i >= minAliasI && tok in aliases -> {
insertTokens(aliases.getValue(tok))
}

canExpandAtFiles
&& tok.startsWith("@")
&& normTok !in optionsByName -> {
Expand Down Expand Up @@ -130,10 +134,6 @@ private class CommandParser<T : BaseCliktCommand<T>>(
consumeOptionParse(parseShortOpt(tok))
}

i >= minAliasI && tok in aliases -> {
insertTokens(aliases.getValue(tok))
}

canParseSubcommands && normTok in allSubcommands -> {
subcommand = allSubcommands.getValue(normTok)
i += 1
Expand Down Expand Up @@ -211,11 +211,7 @@ private class CommandParser<T : BaseCliktCommand<T>>(
}

private fun insertTokens(newTokens: List<String>) {
tokens = buildList(tokens.size + newTokens.size) {
addAll(tokens.take(i))
addAll(newTokens)
addAll(tokens.drop(i + 1))
}
tokens = listOf(tokens.take(i), newTokens, tokens.drop(i + 1)).flatten()
minAliasI = i + newTokens.size
}

Expand Down

0 comments on commit 9f48ab1

Please sign in to comment.