Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {Input,Output}Stream support #157

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<InputStream, InputStream> {
return convert("FILE", completionCandidates = CompletionCandidates.Path) {
if (it == "-") {
System.`in`
} else {
Files.newInputStream(Paths.get(it))
}
}
}

fun NullableOption<InputStream, InputStream>.defaultStdin(): OptionWithValues<InputStream, InputStream, InputStream> {
return default(System.`in`, "-")
}
Original file line number Diff line number Diff line change
@@ -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<OutputStream, OutputStream> {
return convert("FILE", completionCandidates = CompletionCandidates.Path) {
if (it == "-") {
System.out
} else {
Files.newOutputStream(Paths.get(it))
}
}
}

fun NullableOption<OutputStream, OutputStream>.defaultStdout(): OptionWithValues<OutputStream, OutputStream, OutputStream> {
return default(System.out, "-")
}
9 changes: 9 additions & 0 deletions samples/stream/README.md
Original file line number Diff line number Diff line change
@@ -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
```
1 change: 1 addition & 0 deletions samples/stream/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mainClassName = 'com.github.ajalt.clikt.samples.stream.MainKt'
Original file line number Diff line number Diff line change
@@ -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<String>) = Md5Sum().main(args)
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ include 'samples:helpformat'
include 'samples:ansicolors'
include 'samples:plugins'
include 'samples:json'
include 'samples:stream'