Skip to content

Commit 03dfd4c

Browse files
committed
Rename commandHelp -> help
1 parent ad5270c commit 03dfd4c

File tree

16 files changed

+74
-71
lines changed

16 files changed

+74
-71
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- 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.
1313
- Due to changes to the internal parsing algorithm, the exact details of error messages when multiple usage errors occur have changed in some cases.
1414
- **Breaking Change:** Removed the following parameters from `CliktCommand`'s constructor; override the corresponding properties instead:
15-
15+
1616
| removed parameter | replacement property |
1717
|-----------------------------|---------------------------------|
1818
| `help` | `fun commandHelp` |
@@ -23,7 +23,8 @@
2323
| `autoCompleteEnvvar` | `val autoCompleteEnvvar` |
2424
| `allowMultipleSubcommands` | `val allowMultipleSubcommands` |
2525
| `treatUnknownOptionsAsArgs` | `val treatUnknownOptionsAsArgs` |
26-
| `hidden` | `val hidden` |
26+
| `hidden` | `val hiddenFromHelp` |
27+
- The following methods on `CliktCommand` have been renamed: `commandHelp` -> `help`, `commandHelpEpilog` -> `epilog`. The old names are deprecated.
2728

2829
### Fixed
2930
- Fixed excess arguments not being reported when `allowMultipleSubcommands=true` and a subcommand has excess arguments followed by another subcommand.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class CompletionCommand(
3434
private val epilog: String = "",
3535
name: String = "generate-completion",
3636
) : CliktCommand(name) {
37-
override fun commandHelp(context: Context): String = help
38-
override fun commandHelpEpilog(context: Context): String = epilog
37+
override fun help(context: Context): String = help
38+
override fun helpEpilog(context: Context): String = epilog
3939
private val shell by argument("shell").choice(*choices)
4040
override fun run() {
4141
val cmd = currentContext.parent?.command ?: this

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.github.ajalt.clikt.completion
22

33
import com.github.ajalt.clikt.completion.CompletionCandidates.Custom.ShellType
44
import com.github.ajalt.clikt.core.BaseCliktCommand
5-
import com.github.ajalt.clikt.core.CliktCommand
65
import com.github.ajalt.clikt.parameters.options.Option
76

87
internal object FishCompletionGenerator {
@@ -57,7 +56,7 @@ internal object FishCompletionGenerator {
5756

5857
append("-a $commandName ")
5958

60-
val help = command.commandHelp(command.currentContext).replace("'", "\\'")
59+
val help = command.help(command.currentContext).replace("'", "\\'")
6160
if (help.isNotBlank()) {
6261
append("-d '${help}'")
6362
}

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/BaseCliktCommand.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ abstract class BaseCliktCommand<T : BaseCliktCommand<T>>(
2222
*/
2323
name: String? = null,
2424
) : ParameterHolder {
25-
// TODO: rename commandName commandHelp commandHelpEpilog?
2625
/**
2726
* The name of this command, used in help output.
2827
*
@@ -43,15 +42,18 @@ abstract class BaseCliktCommand<T : BaseCliktCommand<T>>(
4342
* }
4443
* ```
4544
*/
46-
open fun commandHelp(context: Context): String = ""
45+
open fun help(context: Context): String = ""
46+
47+
@Deprecated("Method renamed to `help`", ReplaceWith("help(context)"))
48+
open fun commandHelp(context: Context): String = help(context)
4749

4850
/**
4951
* Help text to display at the end of the help output, after any parameters.
50-
*
51-
* You can set this by passing `epilog` to the [CliktCommand] constructor, or by overriding this
52-
* method.
5352
*/
54-
open fun commandHelpEpilog(context: Context): String = ""
53+
open fun helpEpilog(context: Context): String = ""
54+
55+
@Deprecated("Method renamed to `helpEpilog`", ReplaceWith("helpEpilog(context)"))
56+
open fun commandHelpEpilog(context: Context): String = helpEpilog(context)
5557

5658
/**
5759
* Used when this command has subcommands, and this command is called without a subcommand. If
@@ -93,7 +95,7 @@ abstract class BaseCliktCommand<T : BaseCliktCommand<T>>(
9395
/**
9496
* If true, don't display this command in help output when used as a subcommand.
9597
*/
96-
open val hidden: Boolean = false
98+
open val hiddenFromHelp: Boolean = false
9799

98100
// TODO: make these all private?
99101
internal var _subcommands: List<T> = emptyList()
@@ -154,7 +156,7 @@ abstract class BaseCliktCommand<T : BaseCliktCommand<T>>(
154156
c.commandName,
155157
c.shortHelp(currentContext),
156158
c.helpTags
157-
).takeUnless { c.hidden }
159+
).takeUnless { c.hiddenFromHelp }
158160
}
159161
).flatten()
160162
}
@@ -185,7 +187,7 @@ abstract class BaseCliktCommand<T : BaseCliktCommand<T>>(
185187

186188
/** The help displayed in the commands list when this command is used as a subcommand. */
187189
protected fun shortHelp(context: Context): String =
188-
Regex("""\s*(?:```)?\s*(.+)""").find(commandHelp(context))?.groups?.get(1)?.value ?: ""
190+
Regex("""\s*(?:```)?\s*(.+)""").find(help(context))?.groups?.get(1)?.value ?: ""
189191

190192
/** The names of all direct children of this command */
191193
fun registeredSubcommandNames(): List<String> = _subcommands.map { it.commandName }
@@ -279,8 +281,8 @@ abstract class BaseCliktCommand<T : BaseCliktCommand<T>>(
279281
val programName = cmd.getCommandNameWithParents()
280282
return ctx.helpFormatter(ctx).formatHelp(
281283
error as? UsageError,
282-
cmd.commandHelp(ctx),
283-
cmd.commandHelpEpilog(ctx),
284+
cmd.help(ctx),
285+
cmd.helpEpilog(ctx),
284286
cmd.allHelpParams(),
285287
programName
286288
)

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/output/MordantHelpFormatterTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ class MordantHelpFormatterTest {
444444
TestCommand(name = "foo", help = "some thing to live by"),
445445
TestCommand(name = "bar", help = "another argument"),
446446
TestCommand(name = "baz"),
447-
TestCommand(name = "qux", hidden = true),
447+
TestCommand(name = "qux", hiddenFromHelp = true),
448448
)
449449
doTest(
450450
"""
@@ -778,10 +778,10 @@ class MordantHelpFormatterTest {
778778
}
779779

780780
class C: TestCommand(name="prog") {
781-
override fun commandHelp(context: Context): String =
781+
override fun help(context: Context): String =
782782
context.theme.danger("command help")
783783

784-
override fun commandHelpEpilog(context: Context): String =
784+
override fun helpEpilog(context: Context): String =
785785
context.theme.danger("command epilog")
786786

787787
val o by option("--aa", "-a")

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/testing/TestCommand.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ open class TestCommand(
2020
override val autoCompleteEnvvar: String? = "",
2121
override val allowMultipleSubcommands: Boolean = false,
2222
override val treatUnknownOptionsAsArgs: Boolean = false,
23-
override val hidden: Boolean = false,
23+
override val hiddenFromHelp: Boolean = false,
2424
noHelp: Boolean = false,
2525
) : CliktCommand(name) {
2626
init {
@@ -30,9 +30,9 @@ open class TestCommand(
3030
}
3131
}
3232

33-
override fun commandHelp(context: Context): String = help
33+
override fun help(context: Context): String = help
3434

35-
override fun commandHelpEpilog(context: Context): String = epilog
35+
override fun helpEpilog(context: Context): String = epilog
3636

3737
private val count = count ?: if (called) 1 else 0
3838
private var actualCount = 0

docs/commands.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ command names.
101101

102102
=== "Example"
103103
```kotlin
104-
class Tool : CliktCommand(help = "A tool that runs") {
104+
class Tool : CliktCommand() {
105+
override fun help(context: Context) = "A tool that runs"
105106
val verbose by option().flag("--no-verbose")
106107
override fun run() = Unit
107108
}
108109

109-
class Execute : CliktCommand(help = "Execute the command") {
110+
class Execute : CliktCommand() {
111+
override fun help(context: Context) = "Execute the command"
110112
val name by option()
111113
override fun run() = Unit
112114
}
@@ -252,7 +254,7 @@ is about to be invoked, if there is one.
252254
if (subcommand == null) {
253255
echo("invoked without a subcommand")
254256
} else {
255-
echo("about to run ${subcommand.commandName}")
257+
echo("about to run ${subcommand.name}")
256258
}
257259
}
258260
}

docs/documenting.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,40 @@ and set it on the [command's context][customizing-contexts].
77

88
## Help Texts
99

10-
[Commands][Commands] and parameters accept a `help` argument. Commands also accept an `epilog`
11-
argument, which is printed after the parameters and commands on the help page. All text is
12-
automatically trimmed of leading indentation and re-wrapped to the terminal width.
13-
14-
As an alternative to passing your help strings as function arguments, you can also use the `help()`
15-
extensions for your options, and override `commandHelp` and `commandHelpEpilog` on your commands.
16-
These methods can access the terminal theme on the context to add color to your help text.
10+
You can add help text to commands and parameters. For parameters, you can pass a `help` string or
11+
use the `help()` extension. For commands, you can override the `help` and `helpEpilog` methods.
1712

1813
=== "Example"
1914
```kotlin
20-
class Hello : CliktCommand(help = """
15+
class Hello : CliktCommand() {
16+
override fun help(context: Context) = """
2117
This script prints <name> <count> times.
22-
18+
2319
<count> must be a positive number, and defaults to 1.
24-
"""
25-
) {
26-
val count by option("-c", "--count", metavar="count", help = "number of greetings").int().default(1)
27-
val name by argument()
28-
override fun run() = repeat(count) { echo("Hello $name!") }
20+
""".trimIndent()
21+
val count by option("-c", "--count", metavar="count", help="number of greetings")
22+
.int().default(1)
23+
val name by argument(help="The name to greet")
24+
override fun run() = repeat(count) { echo("Hello $commandName!") }
2925
}
3026
```
3127

3228
=== "Alternate style"
3329
```kotlin
3430
class Hello : CliktCommand() {
35-
override fun commandHelp(context: Context): String {
31+
override fun help(context: Context): String {
3632
val style = context.theme.info
3733
return """
38-
This script prints ${style("<name>")} ${style("<count>")} times.
34+
This script prints ${style("<name>")} ${style("<count>")} times.
3935

40-
${style("<count>")} must be a positive number, and defaults to 1.
36+
${style("<count>")} must be a positive number, and defaults to 1.
4137
""".trimIndent()
4238
}
4339

4440
val count by option("-c", "--count", metavar="count").int().default(1)
4541
.help { theme.success("number of greetings") }
4642
val name by argument()
43+
.help("The name to greet")
4744
override fun run() = repeat(count) { echo("Hello $name!") }
4845
}
4946
```
@@ -158,13 +155,17 @@ They have a short help string which is the first line of their help.
158155
```kotlin
159156
class Tool : NoOpCliktCommand()
160157

161-
class Execute : NoOpCliktCommand(help = """
162-
Execute the command.
158+
class Execute : NoOpCliktCommand() {
159+
override fun help(context: Context) = """
160+
Execute the command.
163161

164-
The command will be executed.
165-
""")
162+
The command will be executed.
163+
""".trimIndent()
164+
}
166165

167-
class Abort : NoOpCliktCommand(help="Kill any running commands.")
166+
class Abort : NoOpCliktCommand() {
167+
override fun help(context: Context) = "Kill any running commands."
168+
}
168169
```
169170

170171
=== "Usage"
@@ -463,10 +464,9 @@ You can localize error messages by implementing [`Localization`][Localization] a
463464
// ... override the rest of the strings here
464465
}
465466

466-
class I18NTool : NoOpCliktCommand(help = "𝒯𝒽𝒾𝓈 𝓉𝑜𝑜𝓁 𝒾𝓈 𝒾𝓃 𝒸𝓊𝓇𝓈𝒾𝓋𝑒") {
467-
init {
468-
context { localization = CursiveLocalization() }
469-
}
467+
class I18NTool : NoOpCliktCommand() {
468+
override fun help(context: Context) = "𝒯𝒽𝒾𝓈 𝓉𝑜𝑜𝓁 𝒾𝓈 𝒾𝓃 𝒸𝓊𝓇𝓈𝒾𝓋𝑒"
469+
init { context { localization = CursiveLocalization() } }
470470
}
471471
```
472472

docs/options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ You can define your own version option like this:
868868
class Cli : NoOpCliktCommand() {
869869
init {
870870
eagerOption("--version") {
871-
throw PrintMessage("$commandName version 1.0")
871+
throw PrintMessage("$name version 1.0")
872872
}
873873
}
874874
}

samples/aliases/src/main/kotlin/com/github/ajalt/clikt/samples/aliases/main.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import java.io.File
1111
* have multiple tokens (e.g. `cm = commit -m`).
1212
*/
1313
class AliasedCli(private val configFile: File) : NoOpCliktCommand() {
14-
override fun commandHelp(context: Context) = "An example that supports aliased subcommands"
14+
override fun help(context: Context) = "An example that supports aliased subcommands"
1515
override fun aliases(): Map<String, List<String>> {
1616
return configFile.readLines().map { it.split("=", limit = 2) }
1717
.associate { it[0].trim() to it[1].trim().split(Regex("\\s+")) }
@@ -20,22 +20,22 @@ class AliasedCli(private val configFile: File) : NoOpCliktCommand() {
2020

2121

2222
class Push : CliktCommand() {
23-
override fun commandHelp(context: Context) = "push changes"
23+
override fun help(context: Context) = "push changes"
2424
override fun run() = echo("push")
2525
}
2626

2727
class Pull : CliktCommand() {
28-
override fun commandHelp(context: Context) = "pull changes"
28+
override fun help(context: Context) = "pull changes"
2929
override fun run() = echo("pull")
3030
}
3131

3232
class Clone : CliktCommand() {
33-
override fun commandHelp(context: Context) = "clone a repository"
33+
override fun help(context: Context) = "clone a repository"
3434
override fun run() = echo("clone")
3535
}
3636

3737
class Commit : CliktCommand() {
38-
override fun commandHelp(context: Context) = "clone a repository"
38+
override fun help(context: Context) = "clone a repository"
3939
val message by option("-m", "--message").multiple()
4040
override fun run() = echo("commit message=${message.joinToString("\n")}")
4141
}

0 commit comments

Comments
 (0)