Skip to content

Commit 630ca22

Browse files
committed
Add err parameter to echo
1 parent b08781f commit 630ca22

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ object TermUi {
1313
* This is similar to [print] or [println], but converts newlines to the system line separator.
1414
*
1515
* @param message The message to print.
16-
* @param trailingNewline if true, behave like [println], otherwise behave like [print]
16+
* @param trailingNewline If true, behave like [println], otherwise behave like [print]
17+
* @param err If true, print to stderr instead of stdout
1718
*/
18-
fun echo(message: Any?, trailingNewline: Boolean = true) {
19+
fun echo(message: Any?, trailingNewline: Boolean = true, err: Boolean = false) {
1920
val text = message?.toString()?.replace(Regex("\r?\n"), System.lineSeparator()) ?: "null"
20-
if (trailingNewline) println(text) else print(text)
21+
val ps = if (err) System.err else System.out
22+
if (trailingNewline) ps.println(text) else ps.print(text)
2123
}
2224

2325
/**

docs/_data/sidebars/clikt_sidebar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ entries:
99

1010
- title: Basic Concepts
1111
url: /quickstart.html#basic-concepts
12-
- title: Echoing
12+
- title: Printing to Stdout and Stderr
1313
url: /quickstart.html#echoing
1414
- title: Nesting Commands
1515
url: /quickstart.html#nesting-commands

docs/pages/quickstart.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Options:
4242
-h, --help Show this message and exit
4343
```
4444

45-
## Echoing
45+
## Printing to Stdout and Stderr {#echoing}
4646

4747
Why does this example use {% include api.html pkg="output"
4848
class="term-ui" fun="echo" %} instead of
@@ -52,7 +52,8 @@ support. {% include api.html pkg="output" class="term-ui" fun="echo"
5252
text="echo"%} automatically translates line breaks into the line
5353
separator for the current platform. So you don't have to worry that some
5454
of your users will see mangled output because you didn't test on
55-
Windows.
55+
Windows. You can also pass `err=true` to `echo` to print to stderr
56+
instead of stdout.
5657

5758
## Nesting Commands
5859

0 commit comments

Comments
 (0)