Zusätzliche Kommandozeilen-Parameter via Pipe
This commit is contained in:
1 parent
9df3e0701d
commit
b580e64e76
16 files changed
+127
-39
No files matched your search
@@ -2,8 +2,28 @@ package net.keinekekse.args
|
||||
|
||||
import net.keinekekse.version.Version
|
||||
|
||||
/**
|
||||
* Terminates the currently running application with the specified exit code.
|
||||
* This function does not return as it stops the application process.
|
||||
*
|
||||
* Works only for JVM and macOS-/Linux-native targets.
|
||||
*
|
||||
* @param rc The exit code to return when the application terminates. Defaults to 0,
|
||||
* indicating normal termination.
|
||||
* @return This function does not return as the application is terminated.
|
||||
*/
|
||||
expect fun exitApplication(rc: Int = 0): Nothing
|
||||
|
||||
/**
|
||||
* Checks if standard input is ready for reading.
|
||||
* This function is used to determine if there is data available to be read from standard input.
|
||||
*
|
||||
* Works only for JVM and macOS-/Linux-native targets.
|
||||
*
|
||||
* @return True if standard input is ready for reading, false otherwise.
|
||||
*/
|
||||
expect fun isStdInputReady(): Boolean
|
||||
|
||||
class VerifyParameterException(message: String) : RuntimeException(message)
|
||||
|
||||
fun showParsingError(msg: String): Nothing {
|
||||
@@ -196,7 +216,12 @@ class CommandLine(
|
||||
val resultSubCommand = parseSubCommand(remaining)
|
||||
validateMandatoryParameters(resultSubCommand)
|
||||
|
||||
if (resultSubCommand.additionalParameters.isNotEmpty() && remainingDescription == null) {
|
||||
if (remainingDescription != null) {
|
||||
val additionalFromPipe = getRemainingParametersFromPipe()
|
||||
if (additionalFromPipe.isNotEmpty()) {
|
||||
resultSubCommand.additionalParameters += additionalFromPipe
|
||||
}
|
||||
} else if (resultSubCommand.additionalParameters.isNotEmpty()) {
|
||||
throw ArgumentParserException.noMoreOptions()
|
||||
}
|
||||
|
||||
@@ -261,6 +286,19 @@ class CommandLine(
|
||||
}
|
||||
}
|
||||
|
||||
private fun getRemainingParametersFromPipe(): List<String> {
|
||||
if (isStdInputReady()) {
|
||||
var input: String?
|
||||
val pipe = mutableListOf<String>()
|
||||
do {
|
||||
input = readlnOrNull()
|
||||
input?.let { pipe.add(it) }
|
||||
} while (!input.isNullOrEmpty())
|
||||
return pipe
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
private fun checkExecutors() {
|
||||
globalSubCommand.obj.checkExecutors()
|
||||
subCommands.values.forEach { it.checkExecutors() }
|
||||
@@ -282,7 +320,7 @@ class CommandLine(
|
||||
val globalParam = globalSubCommand.obj.params.isNotEmpty()
|
||||
val remain = remainingDescription != null
|
||||
|
||||
println("${programVersion.displayName} ${programVersion.version}, ${programVersion.copyright}\n")
|
||||
println("${programVersion.displayName} ${programVersion.version} ${programVersion.copyright}\n")
|
||||
println(programVersion.description)
|
||||
println()
|
||||
println(
|
||||
@@ -346,7 +384,7 @@ class CommandLine(
|
||||
}
|
||||
|
||||
private fun showVersion(): Nothing {
|
||||
println("${programVersion.displayName} ${programVersion.fullVersion}, ${programVersion.copyright}\n")
|
||||
println("${programVersion.displayName} ${programVersion.fullVersion} ${programVersion.copyright}\n")
|
||||
exitApplication(0)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user