Skip to content

Commit

Permalink
Add err parameter to echo
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Apr 15, 2018
1 parent b08781f commit 630ca22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
8 changes: 5 additions & 3 deletions clikt/src/main/kotlin/com/github/ajalt/clikt/output/TermUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ object TermUi {
* This is similar to [print] or [println], but converts newlines to the system line separator.
*
* @param message The message to print.
* @param trailingNewline if true, behave like [println], otherwise behave like [print]
* @param trailingNewline If true, behave like [println], otherwise behave like [print]
* @param err If true, print to stderr instead of stdout
*/
fun echo(message: Any?, trailingNewline: Boolean = true) {
fun echo(message: Any?, trailingNewline: Boolean = true, err: Boolean = false) {
val text = message?.toString()?.replace(Regex("\r?\n"), System.lineSeparator()) ?: "null"
if (trailingNewline) println(text) else print(text)
val ps = if (err) System.err else System.out
if (trailingNewline) ps.println(text) else ps.print(text)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/sidebars/clikt_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ entries:

- title: Basic Concepts
url: /quickstart.html#basic-concepts
- title: Echoing
- title: Printing to Stdout and Stderr
url: /quickstart.html#echoing
- title: Nesting Commands
url: /quickstart.html#nesting-commands
Expand Down
5 changes: 3 additions & 2 deletions docs/pages/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Options:
-h, --help Show this message and exit
```

## Echoing
## Printing to Stdout and Stderr {#echoing}

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

## Nesting Commands

Expand Down

0 comments on commit 630ca22

Please sign in to comment.