From 0fc72f23a8b7b64cbd1a39e32321dd9a606082c8 Mon Sep 17 00:00:00 2001 From: Andreas Deininger Date: Thu, 5 Nov 2020 17:59:15 +0100 Subject: [PATCH] Quick guide, subcommands example: add Kotlin + Scala versions --- docs/quick-guide.adoc | 96 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 5 deletions(-) diff --git a/docs/quick-guide.adoc b/docs/quick-guide.adoc index 7723bea4..453bbe91 100644 --- a/docs/quick-guide.adoc +++ b/docs/quick-guide.adoc @@ -7,7 +7,8 @@ :numbered: :toclevels: 1 // show little detail in the TOC, to make this document less intimidating :toc-title: Features -:source-highlighter: coderay +:source-highlighter: rouge +:rouge-languages: kotlin, groovy, scala :icons: font :imagesdir: images :linkattrs: @@ -196,8 +197,8 @@ ISOCodeResolver country cn fr th ro no === ISOCodeResolver source code explained -.ISOCodeResolver source code -[source,java] +.Java +[source,java,role="primary"] ---- import picocli.CommandLine; import picocli.CommandLine.Command; @@ -225,8 +226,8 @@ public class ISOCodeResolver { // <1> } public static void main(String[] args) { - int exitCode = new CommandLine(new ISOCodeResolver()).execute(args); // <5> - System.exit(exitCode); // <6> + int exitCode = new CommandLine(new ISOCodeResolver()).execute(args); // <5> + System.exit(exitCode); // <6> } } @@ -247,6 +248,91 @@ class SubcommandAsClass implements Runnable { } ---- +.Kotlin +[source,kotlin,role="secondary"] +---- +import picocli.CommandLine +import picocli.CommandLine.Model.CommandSpec +import picocli.CommandLine.* +import java.util.Locale +import kotlin.system.exitProcess + +@Command( + name = "ISOCodeResolver", + subcommands = [SubcommandAsClass::class, HelpCommand::class], // <2> + description = ["Resolves ISO country codes (ISO-3166-1) or language codes (ISO 639-1/-2)"]) +class ISOCodeResolver { // <1> + @Spec lateinit var spec: CommandSpec + + @Command(name = "country", description = ["Resolves ISO country codes (ISO-3166-1)"]) // <3> + fun subCommandViaMethod(@Parameters(arity = "1..*", paramLabel = "", + description = ["country code(s) to be resolved"]) countryCodes: Array) { + for (code in countryCodes) { + println("${code.toUpperCase()}: ${Locale("", code).displayCountry}") + } + } +} + +fun main(args: Array) { + val exitCode = CommandLine(ISOCodeResolver()).execute(*args) // <5> + exitProcess(exitCode) // <6> +} + +@Command(name = "language", description = ["Resolves ISO language codes (ISO-639-1/-2)"]) // <4> +class SubcommandAsClass : Runnable { + @Parameters(arity = "1..*", paramLabel = "", description = ["language code(s)"]) + private lateinit var languageCodes: Array + + override fun run() { + for (code in languageCodes) { + System.out.println("${code.toLowerCase()}: ${Locale(code).displayLanguage}") + } + } +} +---- + +.Scala +[source,scala,role="secondary"] +---- +import picocli.CommandLine +import picocli.CommandLine.{Command, HelpCommand, Parameters} +import picocli.CommandLine.Model.CommandSpec +import java.util.Locale + +@Command(name = "ISOCodeResolver", subcommands = Array(classOf[SubcommandAsClass], classOf[HelpCommand]), // <2> + description = Array("Resolves ISO country codes (ISO-3166-1) or language codes (ISO 639-1/-2)")) +class ISOCodeResolver { // <1> + val spec: CommandSpec = null + + @Command(name = "country", description = Array("Resolves ISO country codes (ISO-3166-1)")) // <3> + def subCommandViaMethod(@Parameters(arity = "1..*", paramLabel = "", + description = Array("country code(s) to be resolved")) countryCodes: Array[String]): Unit = { + for (code <- countryCodes) { + println(s"${code.toUpperCase()}: ".concat(new Locale("", code).getDisplayCountry)) + } + } +} + +@Command(name = "language", description = Array("Resolves language codes (ISO-639-1/-2)")) // <4> +class SubcommandAsClass extends Runnable { + @Parameters(arity = "1..*", paramLabel = "", description = Array("language code(s)")) + private val languageCodes = new Array[String](0) + + override def run(): Unit = { + for (code <- languageCodes) { + println(s"${code.toUpperCase()}: ".concat(new Locale(code).getDisplayLanguage)) + } + } +} + +object ISOCodeResolver { + def main(args: Array[String]): Unit = { + val exitCode = new CommandLine(new ISOCodeResolver).execute(args: _*) // <5> + System.exit(exitCode) // <6> + } +} +---- + Let's break it down into small steps: <1> When the top-level command does not implement `Runnable` or `Callable`, users must specify a subcommand (subcommands become mandatory).