diff --git a/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/inputStream.kt b/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/inputStream.kt new file mode 100644 index 000000000..26a5b5324 --- /dev/null +++ b/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/inputStream.kt @@ -0,0 +1,25 @@ +package com.github.ajalt.clikt.parameters.types + +import com.github.ajalt.clikt.completion.CompletionCandidates +import com.github.ajalt.clikt.parameters.options.NullableOption +import com.github.ajalt.clikt.parameters.options.OptionWithValues +import com.github.ajalt.clikt.parameters.options.RawOption +import com.github.ajalt.clikt.parameters.options.convert +import com.github.ajalt.clikt.parameters.options.default +import java.io.InputStream +import java.nio.file.Files +import java.nio.file.Paths + +fun RawOption.inputStream(): NullableOption { + return convert("FILE", completionCandidates = CompletionCandidates.Path) { + if (it == "-") { + System.`in` + } else { + Files.newInputStream(Paths.get(it)) + } + } +} + +fun NullableOption.defaultStdin(): OptionWithValues { + return default(System.`in`, "-") +} diff --git a/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/outputStream.kt b/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/outputStream.kt new file mode 100644 index 000000000..585a9272a --- /dev/null +++ b/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/outputStream.kt @@ -0,0 +1,25 @@ +package com.github.ajalt.clikt.parameters.types + +import com.github.ajalt.clikt.completion.CompletionCandidates +import com.github.ajalt.clikt.parameters.options.NullableOption +import com.github.ajalt.clikt.parameters.options.OptionWithValues +import com.github.ajalt.clikt.parameters.options.RawOption +import com.github.ajalt.clikt.parameters.options.convert +import com.github.ajalt.clikt.parameters.options.default +import java.io.OutputStream +import java.nio.file.Files +import java.nio.file.Paths + +fun RawOption.outputStream(): NullableOption { + return convert("FILE", completionCandidates = CompletionCandidates.Path) { + if (it == "-") { + System.out + } else { + Files.newOutputStream(Paths.get(it)) + } + } +} + +fun NullableOption.defaultStdout(): OptionWithValues { + return default(System.out, "-") +} diff --git a/samples/stream/README.md b/samples/stream/README.md new file mode 100644 index 000000000..0fdeba564 --- /dev/null +++ b/samples/stream/README.md @@ -0,0 +1,9 @@ +# Stream sample + +This example demonstrates the ability to stream data using stdin. + +``` +$ echo 'hello world' > input.txt +$ cat input.txt | ./runsample stream + md5sum: d41d8cd98f00b204e9800998ecf8427e +``` diff --git a/samples/stream/build.gradle b/samples/stream/build.gradle new file mode 100644 index 000000000..ff3df5816 --- /dev/null +++ b/samples/stream/build.gradle @@ -0,0 +1 @@ +mainClassName = 'com.github.ajalt.clikt.samples.stream.MainKt' diff --git a/samples/stream/src/main/kotlin/com/github/ajalt/clikt/samples/stream/main.kt b/samples/stream/src/main/kotlin/com/github/ajalt/clikt/samples/stream/main.kt new file mode 100644 index 000000000..91f4fe02f --- /dev/null +++ b/samples/stream/src/main/kotlin/com/github/ajalt/clikt/samples/stream/main.kt @@ -0,0 +1,35 @@ +package com.github.ajalt.clikt.samples.stream + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.defaultStdin +import com.github.ajalt.clikt.parameters.types.inputStream +import java.security.DigestInputStream +import java.security.MessageDigest + +class Md5Sum : CliktCommand(help = "Compute MD5 checksum") { + + private val input by option().inputStream().defaultStdin() + + override fun run() { + val digest = MessageDigest.getInstance("MD5") + + DigestInputStream(input, digest).use { input -> + var read = input.read() + + while (read != -1) { + read = input.read() + } + } + + val hash = digest.digest() + + val sum = hash.joinToString(separator = "") { + "%02x".format(it.toInt() and 0xFF) + } + + echo("md5sum: $sum") + } +} + +fun main(args: Array) = Md5Sum().main(args) diff --git a/settings.gradle b/settings.gradle index 1e4704602..f72240aa2 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,4 +9,5 @@ include 'samples:helpformat' include 'samples:ansicolors' include 'samples:plugins' include 'samples:json' +include 'samples:stream'