Skip to content

Commit d1bccd6

Browse files
committed
Fix multi-line input when stdin is redirected
Fixes #32
1 parent 63ff88c commit d1bccd6

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### Added
55
- `.multiple().unique()` modifier for options and arguments.
66

7+
### Fixed
8+
- Support multi-line input when redirecting stdin
9+
710
## [1.5.0] - 2018-08-26
811
### Added
912
- Ability to use alternate output streams rather than stdin and stdout by setting `Context.console` or by passing a console to `TermUI` functions.

clikt/src/main/kotlin/com/github/ajalt/clikt/core/Context.kt

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Context(val parent: Context?,
102102
*
103103
* The default uses [System.in] and [System.out].
104104
*/
105-
var console: CliktConsole = defaultCliktConsole()
105+
var console: CliktConsole = parent?.console ?: defaultCliktConsole()
106106
}
107107

108108
companion object {

clikt/src/main/kotlin/com/github/ajalt/clikt/output/CliktConsole.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ class InteractiveCliktConsole(private val console: Console) : CliktConsole {
5656
}
5757

5858
class NonInteractiveCliktConsole : CliktConsole {
59+
private val inReader by lazy { System.`in`.bufferedReader() }
60+
5961
override fun promptForLine(prompt: String, hideInput: Boolean) = try {
6062
print(prompt, false)
61-
System.`in`.bufferedReader().readLine()
63+
inReader.readLine() ?: throw RuntimeException("EOF")
6264
} catch (err: IOException) {
63-
null
65+
throw err
6466
}
6567

6668
override fun print(text: String, error: Boolean) {

clikt/src/test/kotlin/com/github/ajalt/clikt/parameters/PromptOptionsTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ class PromptOptionsTest {
4343

4444
@Test
4545
fun `prompt option`() {
46-
stdin.provideLines("bar")
46+
stdin.provideLines("foo", "bar")
4747

4848
class C : CliktCommand() {
4949
val foo by option().prompt()
50+
val bar by option().prompt()
5051
override fun run() {
51-
foo shouldBe "bar"
52+
foo shouldBe "foo"
53+
bar shouldBe "bar"
5254
}
5355
}
5456
C().parse(emptyArray())
55-
stdout.logWithNormalizedLineSeparator shouldBe "Foo: "
57+
stdout.logWithNormalizedLineSeparator shouldBe "Foo: Bar: "
5658
}
5759

5860
@Test

0 commit comments

Comments
 (0)