Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support aliasing options to other options #536

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading