kotlinx-cli icon indicating copy to clipboard operation
kotlinx-cli copied to clipboard

Print the help message from subcommand

Open grahamjenson opened this issue 1 year ago • 2 comments

The makeUsage command is private it should be protected so that sub classes can call it.

My usecase is that I have a subcommand that itself has subcommands, and if executed without a subcommand I want it to print the help message, e.g. cli sc prints help cli sc ssc does something.

My workaround is this.

import kotlinx.cli.*
import kotlin.reflect.full.*

    class NewSubCommand: Subcommand("sc", "sc") {
        override fun execute() {
            // Print Help
            ArgParser::class.declaredFunctions.find { it.name == "makeUsage" }?.let {
                println(it.call(tiz))
            }
        }
    }

Another workaround might be to throw an error with:

        override fun execute() {
            throw ParsingException("Needs Sub command")
        }

but I think this is also not a great workaround.

grahamjenson avatar Jan 09 '23 19:01 grahamjenson

Hello. Is your feature request the same as https://github.com/Kotlin/kotlinx-cli/issues/60?

SvyatoslavScherbina avatar Jan 10 '23 14:01 SvyatoslavScherbina

Looks to be a possible solution for #60. Thanks for the workarounds, the first worked well for me (slight typo, should be this instead of tiz I think)

gravitylow avatar Jun 25 '23 00:06 gravitylow