Zusätzliche Kommandozeilen-Parameter via Pipe

This commit is contained in:
Hartmut Bauer committed 2026-01-19 00:31:17 +01:00
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)
}
@@ -3,14 +3,14 @@ package net.keinekekse.util
import net.keinekekse.version.Version
object VERSION: Version {
override val version = "0.3.0-beta.2+build.111"
override val fullVersion = "0.3.0-beta.2+build.111"
override val version = "0.3.0-beta.5+build.114"
override val fullVersion = "0.3.0-beta.5+build.114"
override val major = 0
override val minor = 3
override val patch = 0
override val preRelease = "beta.2"
override val buildMetadata = "build.111"
override val buildDate = "2026-01-18T16:59:47.995114Z"
override val preRelease = "beta.5"
override val buildMetadata = "build.114"
override val buildDate = "2026-01-18T22:38:13.309402Z"
override val displayName = "Utility Library"
override val copyright = "Copyright (c) 2025-2026 Hartmut Bauer"
override val name = "util"
@@ -0,0 +1,9 @@
package net.keinekekse.args
import kotlin.system.exitProcess
actual fun isStdInputReady(): Boolean =
(System.`in`.available() > 0)
actual fun exitApplication(rc: Int): Nothing =
exitProcess(rc)
@@ -1,7 +0,0 @@
package net.keinekekse.args
import kotlin.system.exitProcess
actual fun exitApplication(rc: Int): Nothing {
exitProcess(rc)
}
@@ -1,8 +0,0 @@
package net.keinekekse.args
import platform.posix.exit
actual fun exitApplication(rc: Int): Nothing {
exit(rc)
return Unit as Nothing
}
@@ -1,8 +0,0 @@
package net.keinekekse.args
import platform.posix.exit
actual fun exitApplication(rc: Int): Nothing {
exit(rc)
return Unit as Nothing
}
@@ -0,0 +1,22 @@
package net.keinekekse.args
import kotlinx.cinterop.*
import platform.posix.*
// macOS + Linux
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
actual fun isStdInputReady(): Boolean {
return memScoped {
val pfd = alloc<pollfd>()
pfd.fd = STDIN_FILENO
pfd.events = POLLIN.toShort()
// timeout 0 means return immediately
poll(pfd.ptr.reinterpret(), 1u, 10) > 0
}
}
actual fun exitApplication(rc: Int): Nothing {
exit(rc)
return Unit as Nothing
}
@@ -1,5 +1,9 @@
package net.keinekekse.args
actual fun isStdInputReady(): Boolean {
TODO("Not implemented on this platform")
}
actual fun exitApplication(rc: Int): Nothing {
TODO("Not implemented on this platform")
}
@@ -0,0 +1,11 @@
package net.keinekekse.args
// js + wasmJs
actual fun isStdInputReady(): Boolean {
TODO("Not implemented for this platform")
}
actual fun exitApplication(rc: Int): Nothing {
TODO("Not implemented for this platform")
}
@@ -1,5 +0,0 @@
package net.keinekekse.args
actual fun exitApplication(rc: Int): Nothing {
TODO("Not yet implemented") // js + wasmJs
}