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

Kotlin sample code, documentation improvements #1336

Merged
merged 3 commits into from
Mar 4, 2021
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Picocli-based applications can easily [integrate](https://picocli.info/#_depende
The [Micronaut](https://micronaut.io/) microservices framework has [built-in support](https://docs.micronaut.io/latest/guide/index.html#commandLineApps) for picocli.
Picocli ships with a [`picocli-spring-boot-starter` module](https://github.com/remkop/picocli/tree/master/picocli-spring-boot-starter)
that includes a `PicocliSpringFactory` and Spring Boot auto-configuration to use Spring dependency injection in your picocli command line application.
The user manual has an [example](https://picocli.info/#_guice_example) of integrating with Guice.
The user manual has examples of integrating with [Guice](https://picocli.info/#_guice_example), [Spring Boot](https://picocli.info/#_spring_boot_example), [Micronaut](https://picocli.info/#_micronaut_example), [Quarkus](https://picocli.info/#_quarkus_example) and with containers that comply to [CDI 2.0 specification](https://picocli.info/#_cdi_2_0_jsr_365) (JSR 365).

### Releases
* [All Releases](https://github.com/remkop/picocli/releases)
Expand Down Expand Up @@ -106,7 +106,6 @@ The user manual has an [example](https://picocli.info/#_guice_example) of integr
* [CLI applications with GraalVM Native Image](https://medium.com/graalvm/cli-applications-with-graalvm-native-image-d629a40aa0be) (2020-11-13) by [Oleg Šelajev](https://twitter.com/shelajev).
* [Picocli subcommands - One program, many purposes](https://aragost.com/blog/java/picocli-subcommands.html) (2020-09-22) by [Jonas Andersen](https://twitter.com/PrimusAlgo).
* [How to build a CLI app in Java using jbang and picocli](https://www.twilio.com/blog/cli-app-java-jbang-picocli) (2020-08-13) by [Matthew Gilliard](https://twitter.com/MaximumGilliard).
* [VIDEO] [Tuesday Java Lunch & Learn. This week: picocli](https://m.twitch.tv/videos/706906131) (2020-08-11) by MaximumGilliard - twitch.tv.
* [Building a GitHub Dependents Scraper with Quarkus and Picocli](https://blog.marcnuri.com/github-dependents-scraper-quarkus-picocli/) (2020-07-31) by [Marc Nuri](https://twitter.com/MarcNuri).
* [Building a decent Java CLI](https://atextor.de/2020/07/27/building-a-decent-java-cli.html) (2020-07-27) by [Andreas Textor](https://twitter.com/atextor).
* [VIDEO] (Another very well-produced video by Szymon Stepniak) [OAuth 2.0 in a Java command-line app | #micronaut #picocli #oauth2](https://www.youtube.com/watch?v=js5H9UbmmMY) (2020-07-23) by [Szymon Stepniak](https://e.printstacktrace.blog/) ([YouTube channel](https://www.youtube.com/channel/UCEf8e5YAYnowq-2deW4tpsw)).
Expand Down
43 changes: 39 additions & 4 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11091,7 +11091,7 @@ class MailServiceImpl : IMailService {
<1> The `JavaMailSender` is provided by the Spring framework.
<2> Once a mail was successful sent, a log message will be emitted.

Finally we have to configure our mail service. This can be done inside a file `application properties`, which we put in a `config` subdirectory of your project:
Finally we have to configure our mail service. This can be done inside a file `application.properties`, which we put in a `config` subdirectory of your project:

.application.properties
----
Expand Down Expand Up @@ -11669,7 +11669,8 @@ Additionally, you can do white box testing by keeping a reference to the applica

For example:

[source,java]
.Java
[source,java, role=primary]
----
MyApp app = new MyApp();
CommandLine cmd = new CommandLine(app);
Expand All @@ -11687,10 +11688,30 @@ assertEquals("expectedValue1", app.getState1());
assertEquals("expectedValue2", app.getState2());
----

.Kotlin
[source,kotlin,role="secondary"]
----
val app = MyApp()
val cmd = CommandLine(app)

val sw = StringWriter()
cmd.out = PrintWriter(sw)

// black box testing
val exitCode = cmd.execute("-x", "-y=123")
assertEquals(0, exitCode)
assertEquals("Your output is abc...", sw.toString())

// white box testing
assertEquals("expectedValue1", app.state1)
assertEquals("expectedValue2", app.state2)
----

This assumes that the application uses the `PrintWriter` provided by https://picocli.info/apidocs/picocli/CommandLine.html#getOut--[`CommandLine.getOut`] or https://picocli.info/apidocs/picocli/CommandLine.html#getErr--[`CommandLine.getErr`].
Applications can get these writers via the <<#spec-annotation,`@Spec`>> annotation:

[source,java]
.Java
[source,java,role=primary]
----
@Command
class MyApp implements Runnable {
Expand All @@ -11703,8 +11724,22 @@ class MyApp implements Runnable {
}
----

.Kotlin
[source,kotlin,role=secondary]
----
@Command
class MyApp : Runnable {
@Spec lateinit var spec: CommandSpec

override fun run() {
// make testing easier by printing to the Err and Out streams provided by picocli
spec.commandLine().out.println("Your output is abc...")
}
}
----

=== Testing the Output
Applications that print to `System.out` or `System.err` directly, or use a version of picocli older than 4.0, can be tested by capturing the standard output and error streams.
Applications that print to `System.out` or `System.err` directly, or use a version of picocli lower than 4.0, can be tested by capturing the standard output and error streams.
There are various options for doing this, some of which are shown below.

==== Java 8+ with System-Lambda
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jlineVersion = 2.14.6
jline3Version = 3.18.0
junitDepVersion = 4.11
junitVersion = 4.12
springBootVersion = 2.4.1
springBootVersion = 2.4.3

# projectPreviousReleaseVersion is non-SNAPSHOT, only published releases
projectPreviousReleaseVersion = 4\\.6\\.1
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions picocli-examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"
}
}

Expand Down Expand Up @@ -38,7 +38,7 @@ dependencies {
"org.hibernate.validator:hibernate-validator-annotation-processor:6.1.2.Final",
"javax.el:javax.el-api:3.0.0",
"org.glassfish.web:javax.el:2.2.6"
implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.4.21"
implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.4.31"
}
tasks.withType(GroovyCompile) {
// this, and the `configurations {ivy}` section, are a workaround for the dreaded
Expand Down