mirror of
https://github.com/corda/corda.git
synced 2025-02-22 02:06:45 +00:00
Add programmatic print help to CordaCliWrapper (#4177)
* add programmatic print help to CordaCliWrapper * address PR issues * bug fix
This commit is contained in:
parent
729b457332
commit
459b330ebd
@ -2,17 +2,15 @@ package net.corda.cliutils
|
|||||||
|
|
||||||
import net.corda.core.internal.rootMessage
|
import net.corda.core.internal.rootMessage
|
||||||
import net.corda.core.utilities.contextLogger
|
import net.corda.core.utilities.contextLogger
|
||||||
import net.corda.core.utilities.loggerFor
|
|
||||||
|
|
||||||
import org.fusesource.jansi.AnsiConsole
|
import org.fusesource.jansi.AnsiConsole
|
||||||
import org.slf4j.event.Level
|
import org.slf4j.event.Level
|
||||||
import picocli.CommandLine
|
import picocli.CommandLine
|
||||||
import picocli.CommandLine.*
|
import picocli.CommandLine.*
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
import kotlin.system.exitProcess
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When we have errors in command line flags that are not handled by picocli (e.g. non existing files), an error is thrown
|
* When we have errors in command line flags that are not handled by picocli (e.g. non existing files), an error is thrown
|
||||||
@ -64,18 +62,6 @@ fun CordaCliWrapper.start(args: Array<String>) {
|
|||||||
// This line makes sure ANSI escapes work on Windows, where they aren't supported out of the box.
|
// This line makes sure ANSI escapes work on Windows, where they aren't supported out of the box.
|
||||||
AnsiConsole.systemInstall()
|
AnsiConsole.systemInstall()
|
||||||
|
|
||||||
val cmd = CommandLine(this)
|
|
||||||
// Make sure any provided paths are absolute. Relative paths have caused issues and are less clear in logs.
|
|
||||||
cmd.registerConverter(Path::class.java) { Paths.get(it).toAbsolutePath().normalize() }
|
|
||||||
cmd.commandSpec.name(alias)
|
|
||||||
cmd.commandSpec.usageMessage().description(description)
|
|
||||||
this.subCommands().forEach {
|
|
||||||
val subCommand = CommandLine(it)
|
|
||||||
it.args = args
|
|
||||||
subCommand.commandSpec.usageMessage().description(it.description)
|
|
||||||
cmd.commandSpec.addSubcommand(it.alias, subCommand)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val defaultAnsiMode = if (CordaSystemUtils.isOsWindows()) {
|
val defaultAnsiMode = if (CordaSystemUtils.isOsWindows()) {
|
||||||
Help.Ansi.ON
|
Help.Ansi.ON
|
||||||
@ -97,7 +83,7 @@ fun CordaCliWrapper.start(args: Array<String>) {
|
|||||||
exitProcess(ExitCodes.SUCCESS)
|
exitProcess(ExitCodes.SUCCESS)
|
||||||
} catch (e: ExecutionException) {
|
} catch (e: ExecutionException) {
|
||||||
val throwable = e.cause ?: e
|
val throwable = e.cause ?: e
|
||||||
if (this.verbose || this.subCommands().any { it.verbose} ) {
|
if (this.verbose || this.subCommands().any { it.verbose }) {
|
||||||
throwable.printStackTrace()
|
throwable.printStackTrace()
|
||||||
} else {
|
} else {
|
||||||
System.err.println("*ERROR*: ${throwable.rootMessage ?: "Use --verbose for more details"}")
|
System.err.println("*ERROR*: ${throwable.rootMessage ?: "Use --verbose for more details"}")
|
||||||
@ -171,6 +157,21 @@ abstract class CordaCliWrapper(alias: String, description: String) : CliWrapperB
|
|||||||
|
|
||||||
protected open fun additionalSubCommands(): Set<CliWrapperBase> = emptySet()
|
protected open fun additionalSubCommands(): Set<CliWrapperBase> = emptySet()
|
||||||
|
|
||||||
|
val cmd by lazy {
|
||||||
|
CommandLine(this).apply {
|
||||||
|
// Make sure any provided paths are absolute. Relative paths have caused issues and are less clear in logs.
|
||||||
|
registerConverter(Path::class.java) { Paths.get(it).toAbsolutePath().normalize() }
|
||||||
|
commandSpec.name(alias)
|
||||||
|
commandSpec.usageMessage().description(description)
|
||||||
|
subCommands().forEach {
|
||||||
|
val subCommand = CommandLine(it)
|
||||||
|
it.args = args
|
||||||
|
subCommand.commandSpec.usageMessage().description(it.description)
|
||||||
|
commandSpec.addSubcommand(it.alias, subCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun subCommands(): Set<CliWrapperBase> {
|
fun subCommands(): Set<CliWrapperBase> {
|
||||||
return additionalSubCommands() + installShellExtensionsParser
|
return additionalSubCommands() + installShellExtensionsParser
|
||||||
}
|
}
|
||||||
@ -181,6 +182,8 @@ abstract class CordaCliWrapper(alias: String, description: String) : CliWrapperB
|
|||||||
installShellExtensionsParser.updateShellExtensions()
|
installShellExtensionsParser.updateShellExtensions()
|
||||||
return runProgram()
|
return runProgram()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun printHelp() = cmd.usage(System.out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user