Skip to content

Commit

Permalink
Add reusable main implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Apr 28, 2024
1 parent 5f73201 commit 88450a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/BaseCliktCommand.kt
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,15 @@ abstract class BaseCliktCommand<RunnerT : Function<*>>(
}

/** Add the given commands as a subcommand of this command. */
fun <RunnerT, BaseT : BaseCliktCommand<RunnerT>, CommandT : BaseT> CommandT.subcommands(
commands: Iterable<BaseT>,
fun <RunnerT : Function<*>, CommandT : BaseCliktCommand<RunnerT>> CommandT.subcommands(
commands: Iterable<BaseCliktCommand<RunnerT>>,
): CommandT = apply {
_subcommands = _subcommands + commands
}

/** Add the given commands as a subcommand of this command. */
fun <RunnerT, BaseT : BaseCliktCommand<RunnerT>, CommandT : BaseT> CommandT.subcommands(
vararg commands: BaseT,
fun <RunnerT : Function<*>, CommandT : BaseCliktCommand<RunnerT>> CommandT.subcommands(
vararg commands: BaseCliktCommand<RunnerT>,
): CommandT = apply {
_subcommands = _subcommands + commands
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.ajalt.clikt.core

import com.github.ajalt.clikt.mpp.exitProcessMpp
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parsers.CommandLineParser
Expand Down Expand Up @@ -38,7 +37,7 @@ abstract class CliktCommand(
treatUnknownOptionsAsArgs,
hidden
) {
final override val runner: () -> Unit = ::run
final override val runner: () -> Unit get() = ::run

/**
* Perform actions after parsing is complete and this command is invoked.
Expand Down Expand Up @@ -72,14 +71,7 @@ fun BaseCliktCommand<() -> Unit>.main(argv: Array<out String>) = main(argv.asLis
*
* If you don't want Clikt to exit your process, call [parse] instead.
*/
fun BaseCliktCommand<() -> Unit>.main(argv: List<String>) {
try {
parse(argv)
} catch (e: CliktError) {
echoFormattedHelp(e)
exitProcessMpp(e.statusCode)
}
}
fun BaseCliktCommand<() -> Unit>.main(argv: List<String>) = main(argv) { parse(argv) }

/**
* Parse the command line and throw an exception if parsing fails.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.ajalt.clikt.core

import com.github.ajalt.clikt.mpp.exitProcessMpp

// TODO: docs
fun <RunnerT : Function<*>> BaseCliktCommand<RunnerT>.main(
argv: List<String>,
parse: BaseCliktCommand<RunnerT>.(List<String>) -> Unit,
) {
try {
parse(argv)
} catch (e: CliktError) {
echoFormattedHelp(e)
exitProcessMpp(e.statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class CliktCommandTest {
@JsName("treat_unknown_options_as_arguments")
fun `treat unknown options as arguments`() {
class C : TestCommand(treatUnknownOptionsAsArgs = true) {

val foo by option().flag()
val args by argument().multiple()

Expand Down

0 comments on commit 88450a3

Please sign in to comment.