Skip to content

Commit 38c6e81

Browse files
committed
Combine test annotations, reformat all code
1 parent 8f3668b commit 38c6e81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+471
-792
lines changed

clikt-mordant-markdown/src/commonMain/kotlin/com/github/ajalt/clikt/output/MordantMarkdownHelpFormatter.kt

-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
package com.github.ajalt.clikt.output
22

33
import com.github.ajalt.clikt.core.Context
4-
import com.github.ajalt.clikt.core.UsageError
5-
import com.github.ajalt.clikt.core.terminal
6-
import com.github.ajalt.clikt.output.HelpFormatter.ParameterHelp
74
import com.github.ajalt.mordant.markdown.Markdown
8-
import com.github.ajalt.mordant.rendering.Theme
9-
import com.github.ajalt.mordant.rendering.Whitespace
105
import com.github.ajalt.mordant.rendering.Widget
11-
import com.github.ajalt.mordant.table.verticalLayout
12-
import com.github.ajalt.mordant.widgets.Text
13-
import com.github.ajalt.mordant.widgets.definitionList
14-
import com.github.ajalt.mordant.widgets.withPadding
156

167
/**
178
* A [HelpFormatter] that uses Mordant to render its output as GitHub Flavored Markdown.

clikt-mordant/src/commonMain/kotlin/com/github/ajalt/clikt/command/ChainedCliktCommand.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.ajalt.clikt.command
22

3-
import com.github.ajalt.clikt.core.*
3+
import com.github.ajalt.clikt.core.CliktCommand
4+
import com.github.ajalt.clikt.core.installMordant
45
import com.github.ajalt.clikt.parsers.CommandLineParser
56
import com.github.ajalt.clikt.testing.CliktCommandTestResult
67
import com.github.ajalt.clikt.testing.test

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/command/CoreChainedCliktCommand.kt

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ abstract class CoreChainedCliktCommand<T>(
3131
}
3232

3333

34-
3534
/**
3635
* Parse the command line and print helpful output if any errors occur.
3736
*

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/completion/CompletionBuiltins.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ fun <T : BaseCliktCommand<*>> T.completionOption(
2222
help: String = "",
2323
hidden: Boolean = false,
2424
): T = apply {
25-
registerOption(option(
26-
*names, help = help, hidden = hidden, eager = true,
27-
metavar = choices.joinToString("|", prefix = "(", postfix = ")")
28-
).validate {
29-
throw PrintCompletionMessage(generateCompletionForCommand(context.command, it))
30-
})
25+
registerOption(
26+
option(
27+
*names, help = help, hidden = hidden, eager = true,
28+
metavar = choices.joinToString("|", prefix = "(", postfix = ")")
29+
).validate {
30+
throw PrintCompletionMessage(generateCompletionForCommand(context.command, it))
31+
})
3132
}
3233

3334
/**

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/completion/CompletionGenerator.kt

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.ajalt.clikt.completion
22

33
import com.github.ajalt.clikt.core.BaseCliktCommand
4-
import com.github.ajalt.clikt.core.PrintCompletionMessage
54

65
object CompletionGenerator {
76
/**

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/completion/FishCompletionGenerator.kt

+80-79
Original file line numberDiff line numberDiff line change
@@ -11,109 +11,110 @@ internal object FishCompletionGenerator {
1111
}
1212

1313

14-
private fun generateFishCompletionForCommand(command: BaseCliktCommand<*>): String = buildString {
15-
val parentCommandName = command.currentContext.parentNames().lastOrNull()
16-
val rootCommandName = command.currentContext.commandNameWithParents().first()
17-
val isTopLevel = parentCommandName == null
18-
val commandName = command.commandName
19-
val options = command._options.filterNot { it.hidden }
20-
val arguments = command._arguments
21-
val subcommands = command._subcommands
22-
val hasSubcommands = subcommands.isNotEmpty()
23-
val subcommandsVarName =
24-
command.currentContext.commandNameWithParents().subcommandsVarName()
25-
val parentSubcommandsVarName = when {
26-
isTopLevel -> subcommandsVarName
27-
else -> command.currentContext.parentNames().subcommandsVarName()
28-
}
14+
private fun generateFishCompletionForCommand(command: BaseCliktCommand<*>): String =
15+
buildString {
16+
val parentCommandName = command.currentContext.parentNames().lastOrNull()
17+
val rootCommandName = command.currentContext.commandNameWithParents().first()
18+
val isTopLevel = parentCommandName == null
19+
val commandName = command.commandName
20+
val options = command._options.filterNot { it.hidden }
21+
val arguments = command._arguments
22+
val subcommands = command._subcommands
23+
val hasSubcommands = subcommands.isNotEmpty()
24+
val subcommandsVarName =
25+
command.currentContext.commandNameWithParents().subcommandsVarName()
26+
val parentSubcommandsVarName = when {
27+
isTopLevel -> subcommandsVarName
28+
else -> command.currentContext.parentNames().subcommandsVarName()
29+
}
2930

30-
if (isTopLevel) {
31-
appendLine(
32-
"""
31+
if (isTopLevel) {
32+
appendLine(
33+
"""
3334
|# Command completion for $commandName
3435
|# Generated by Clikt
3536
""".trimMargin()
36-
)
37-
}
37+
)
38+
}
3839

39-
if (hasSubcommands || !isTopLevel) {
40-
appendLine("\n\n### Setup for $commandName")
41-
}
40+
if (hasSubcommands || !isTopLevel) {
41+
appendLine("\n\n### Setup for $commandName")
42+
}
4243

43-
if (hasSubcommands) {
44-
val subcommandsStr = subcommands.joinToString(" ") { it.commandName }
45-
appendLine("set -l $subcommandsVarName '$subcommandsStr'")
46-
}
44+
if (hasSubcommands) {
45+
val subcommandsStr = subcommands.joinToString(" ") { it.commandName }
46+
appendLine("set -l $subcommandsVarName '$subcommandsStr'")
47+
}
4748

48-
if (!isTopLevel) {
49-
append("complete -c $rootCommandName -f ")
49+
if (!isTopLevel) {
50+
append("complete -c $rootCommandName -f ")
5051

51-
if (rootCommandName == parentCommandName) {
52-
append("-n __fish_use_subcommand ")
53-
} else {
54-
append("-n \"__fish_seen_subcommand_from $parentCommandName; and not __fish_seen_subcommand_from \$$parentSubcommandsVarName\" ")
55-
}
52+
if (rootCommandName == parentCommandName) {
53+
append("-n __fish_use_subcommand ")
54+
} else {
55+
append("-n \"__fish_seen_subcommand_from $parentCommandName; and not __fish_seen_subcommand_from \$$parentSubcommandsVarName\" ")
56+
}
57+
58+
append("-a $commandName ")
5659

57-
append("-a $commandName ")
60+
val help = command.help(command.currentContext).replace("'", "\\'")
61+
if (help.isNotBlank()) {
62+
append("-d '${help}'")
63+
}
5864

59-
val help = command.help(command.currentContext).replace("'", "\\'")
60-
if (help.isNotBlank()) {
61-
append("-d '${help}'")
65+
appendLine()
6266
}
6367

64-
appendLine()
65-
}
68+
if (options.any { o -> o.allNames.any { it.isValidFishCompletionOption } }) {
69+
appendLine("\n## Options for $commandName")
70+
}
6671

67-
if (options.any { o -> o.allNames.any { it.isValidFishCompletionOption } }) {
68-
appendLine("\n## Options for $commandName")
69-
}
72+
for (option in options) {
73+
val names = option.allNames.filter { it.isValidFishCompletionOption }
74+
if (names.isEmpty()) {
75+
continue
76+
}
7077

71-
for (option in options) {
72-
val names = option.allNames.filter { it.isValidFishCompletionOption }
73-
if (names.isEmpty()) {
74-
continue
75-
}
78+
appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
7679

77-
appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
80+
for (name in names) {
81+
append(' ')
82+
when {
83+
name.startsWith("--") -> append("-l ")
84+
name.length == 2 -> append("-s ")
85+
else -> append("-o ")
86+
}
87+
append(name.trimStart('-'))
88+
}
7889

79-
for (name in names) {
80-
append(' ')
81-
when {
82-
name.startsWith("--") -> append("-l ")
83-
name.length == 2 -> append("-s ")
84-
else -> append("-o ")
90+
if (option.nvalues.first > 0) {
91+
append(" -r")
8592
}
86-
append(name.trimStart('-'))
87-
}
8893

89-
if (option.nvalues.first > 0) {
90-
append(" -r")
94+
appendParamCompletion(option.completionCandidates)
95+
appendHelp(option.optionHelp(command.currentContext))
96+
appendLine()
9197
}
9298

93-
appendParamCompletion(option.completionCandidates)
94-
appendHelp(option.optionHelp(command.currentContext))
95-
appendLine()
96-
}
97-
98-
if (arguments.isNotEmpty()) {
99-
appendLine("\n## Arguments for $commandName")
100-
}
99+
if (arguments.isNotEmpty()) {
100+
appendLine("\n## Arguments for $commandName")
101+
}
101102

102-
for (argument in arguments) {
103-
appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
104-
appendParamCompletion(argument.completionCandidates)
105-
appendHelp(argument.getArgumentHelp(command.currentContext))
106-
appendLine()
107-
}
103+
for (argument in arguments) {
104+
appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
105+
appendParamCompletion(argument.completionCandidates)
106+
appendHelp(argument.getArgumentHelp(command.currentContext))
107+
appendLine()
108+
}
108109

109-
for (subcommand in subcommands) {
110-
append(
111-
generateFishCompletionForCommand(
112-
command = subcommand
110+
for (subcommand in subcommands) {
111+
append(
112+
generateFishCompletionForCommand(
113+
command = subcommand
114+
)
113115
)
114-
)
116+
}
115117
}
116-
}
117118

118119
private fun StringBuilder.appendCompleteCall(
119120
rootCommandName: String,

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/groups/CoOccurringOptionGroup.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class CoOccurringOptionGroup<GroupT : OptionGroup, OutT> internal constructor(
4747

4848
override fun getValue(thisRef: BaseCliktCommand<*>, property: KProperty<*>): OutT = value
4949

50-
override fun finalize(context: Context, invocationsByOption: Map<Option, List<OptionInvocation>>) {
50+
override fun finalize(
51+
context: Context,
52+
invocationsByOption: Map<Option, List<OptionInvocation>>,
53+
) {
5154
occurred = invocationsByOption.isNotEmpty() || group.options.any {
5255
it.hasEnvvarOrSourcedValue(context, invocationsByOption[it] ?: emptyList())
5356
}

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/groups/ParameterGroup.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ open class OptionGroup(
9090
options += option
9191
}
9292

93-
override fun finalize(context: Context, invocationsByOption: Map<Option, List<OptionInvocation>>) {
93+
override fun finalize(
94+
context: Context,
95+
invocationsByOption: Map<Option, List<OptionInvocation>>,
96+
) {
9497
finalizeOptions(context, options, invocationsByOption)
9598
}
9699

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/options/FlagOption.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ inline fun <OutT> OptionWithValues<Boolean, Boolean, Boolean>.convert(
8181
* @param clamp If `true`, the counted value will be clamped to the [limit] if it is exceeded. If
8282
* `false`, an error will be shown isntead of clamping.
8383
*/
84-
fun RawOption.counted(limit: Int = Int.MAX_VALUE, clamp: Boolean = true): OptionWithValues<Int, Int, Int> {
84+
fun RawOption.counted(
85+
limit: Int = Int.MAX_VALUE,
86+
clamp: Boolean = true,
87+
): OptionWithValues<Int, Int, Int> {
8588
return int().transformValues(0..0) { it.lastOrNull() ?: 1 }.transformAll {
8689
val s = it.sum()
8790
if (!clamp && s > limit) {

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/options/TransformAll.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fun <EachT, ValueT> NullableOption<EachT, ValueT>.multiple(
145145
* ```
146146
*/
147147
fun <T, EachT, ValueT> OptionWithValues<List<T>, EachT, ValueT>.unique()
148-
: OptionWithValues<Set<T>, EachT, ValueT> {
148+
: OptionWithValues<Set<T>, EachT, ValueT> {
149149
return copy(transformValue, transformEach, { transformAll(it).toSet() }, defaultValidator())
150150
}
151151

@@ -155,7 +155,7 @@ fun <T, EachT, ValueT> OptionWithValues<List<T>, EachT, ValueT>.unique()
155155
* If the same key appears more than once, the last one will be added to the map.
156156
*/
157157
fun <A, B, EachT, ValueT> OptionWithValues<List<Pair<A, B>>, EachT, ValueT>.toMap()
158-
: OptionWithValues<Map<A, B>, EachT, ValueT> {
158+
: OptionWithValues<Map<A, B>, EachT, ValueT> {
159159
return copy(transformValue, transformEach, { transformAll(it).toMap() }, defaultValidator())
160160
}
161161

@@ -188,7 +188,7 @@ inline fun <K, V> RawOption.associate(
188188
inline fun <K> RawOption.associateBy(
189189
delimiter: String = "=",
190190
crossinline keySelector: (String) -> K,
191-
): OptionWithValues<Map<K, String>, Pair<K, String>, Pair<K, String>> {
191+
): OptionWithValues<Map<K, String>, Pair<K, String>, Pair<K, String>> {
192192
return associate(delimiter) { keySelector(it.first) to it.second }
193193
}
194194

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/options/TransformEach.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ fun <EachT, ValueT> NullableOption<ValueT, ValueT>.transformValues(
2727
transformAll = defaultAllProcessor(),
2828
validator = defaultValidator(),
2929
metavarGetter = when (nvalues) {
30-
0..0 -> { { null } }
31-
else -> { metavarGetter }
30+
0..0 -> {
31+
{ null }
32+
}
33+
34+
else -> {
35+
metavarGetter
36+
}
3237
},
3338
nvalues = nvalues
3439
)

clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/core/ContextJvm.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.github.ajalt.clikt.core
1919
* @see Context.callOnClose
2020
* @see Context.registerCloseable
2121
*/
22-
fun <T: AutoCloseable> Context.registerJvmCloseable(closeable: T): T {
22+
fun <T : AutoCloseable> Context.registerJvmCloseable(closeable: T): T {
2323
callOnClose { closeable.close() }
2424
return closeable
2525
}

test/src/commonTest/kotlin/com/github/ajalt/clikt/command/ChainedCliktCommandTest.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import kotlin.test.Test
1313

1414

1515
class ChainedCliktCommandTest {
16-
@Test
17-
@JsName("chained_run")
16+
@[Test JsName("chained_run")]
1817
fun `chained run`() = runTest {
1918
class C : ChainedCliktCommand<List<Int>>() {
2019
override val allowMultipleSubcommands: Boolean = true
@@ -37,8 +36,7 @@ class ChainedCliktCommandTest {
3736
}
3837

3938

40-
@Test
41-
@JsName("chained_command_context")
39+
@[Test JsName("chained_command_context")]
4240
fun `chained command context`() {
4341
class C : ChainedCliktCommand<Int>() {
4442
val arg by argument().int()
@@ -52,8 +50,7 @@ class ChainedCliktCommandTest {
5250
}
5351

5452

55-
@Test
56-
@JsName("chained_command_test")
53+
@[Test JsName("chained_command_test")]
5754
fun `chained command test`() {
5855
class C : ChainedCliktCommand<Int>() {
5956
val arg by argument().int()

0 commit comments

Comments
 (0)