Picocli: Looking for examples of using picocli from other JVM languages

Created on 12 Sep 2017  路  4Comments  路  Source: remkop/picocli

Picocli is an annotation-based command line parser written in Java that scales down to very small applications, generates customizable usage help with ANSI colors and styles, and features TAB autocompletion(!), nested subcommands (to any depth) and more.

I'm working on the user manual for the upcoming v2.0 release, and I'm looking for examples of how picocli can be used with JVM languages other than Java. (Work in progress is here.)

If you have tips, suggestions or (ideally) code snippets, please add them to this ticket.


doc help wanted

Most helpful comment

Kotlin

https://kotlinlang.org/docs/reference/annotations.html

Kotlin does not allow specifying array annotation attribute as a single value, so be aware that you will have to write arrayOf(...) for the names, description and type attributes.

@Command(name = "MyApp", version = arrayOf("Kotlin picocli demo v1.0"),
        description = arrayOf("@|bold Kotlin|@ @|underline picocli|@ example"))
class MyApp : Runnable {

    @Option(names = arrayOf("-c", "--count"), paramLabel = "COUNT",
            description = arrayOf("the count"))
    private val count: Int = 0

    @Option(names = arrayOf("-h", "--help"), usageHelp = true,
            description = arrayOf("print this help and exit"))
    private val helpRequested: Boolean = false

    @Option(names = arrayOf("-V", "--version"), versionHelp = true,
            description = arrayOf("print version info and exit"))
    private val versionRequested: Boolean = false

    override fun run() {
        if (helpRequested) {
            CommandLine(this).usage(System.err)
        } else if (versionRequested) {
            CommandLine(this).printVersionHelp(System.err)
        } else {
            for (i in 0 until count) {
                println("hello world $i...")
            }
        }
    }
    companion object {
        @JvmStatic fun main(args: Array<String>) {
            CommandLine.run(MyApp(), System.err, *args)
        }
    }
}

Scala

Scala does not allow specifying array annotation attribute as a single value, so be aware that you will have to write Array(...) for the names, description and type attributes.

@Command(name = "MyApp", version = Array("Scala picocli demo v1.0"),
  description = Array("@|bold Scala|@ @|underline picocli|@ example"))
class MyApp extends Runnable {

  @Option(names = Array("-c", "--count"), paramLabel = "COUNT",
    description = Array("the count"))
  private val count: Int = 0

  @Option(names = Array("-h", "--help"), usageHelp = true,
    description = Array("print this help and exit"))
  private val helpRequested: Boolean = false

  @Option(names = Array("-V", "--version"), versionHelp = true,
    description = Array("print version info and exit"))
  private val versionRequested: Boolean = false

  def run() : Unit = {
    if (helpRequested) {
      new CommandLine(this).usage(System.err)
    } else if (versionRequested) {
      new CommandLine(this).printVersionHelp(System.err)
    } else {
      for (i <- 0 until count) {
        println(s"hello world $i...")
      }
    }
  }
}
object MyApp {
  def main(args: Array[String]) {
    CommandLine.run(new MyApp(), System.err, args: _*)
  }
}

Groovy

In Groovy, use [ and ] to surround array values, instead of the { and } used in Java.

class Args {
    @Option(names = ["-h", "--help"], usageHelp=true, 
                   description="Print this help and exit")
    boolean helpRequested = false;
}

All 4 comments

Kotlin

https://kotlinlang.org/docs/reference/annotations.html

Kotlin does not allow specifying array annotation attribute as a single value, so be aware that you will have to write arrayOf(...) for the names, description and type attributes.

@Command(name = "MyApp", version = arrayOf("Kotlin picocli demo v1.0"),
        description = arrayOf("@|bold Kotlin|@ @|underline picocli|@ example"))
class MyApp : Runnable {

    @Option(names = arrayOf("-c", "--count"), paramLabel = "COUNT",
            description = arrayOf("the count"))
    private val count: Int = 0

    @Option(names = arrayOf("-h", "--help"), usageHelp = true,
            description = arrayOf("print this help and exit"))
    private val helpRequested: Boolean = false

    @Option(names = arrayOf("-V", "--version"), versionHelp = true,
            description = arrayOf("print version info and exit"))
    private val versionRequested: Boolean = false

    override fun run() {
        if (helpRequested) {
            CommandLine(this).usage(System.err)
        } else if (versionRequested) {
            CommandLine(this).printVersionHelp(System.err)
        } else {
            for (i in 0 until count) {
                println("hello world $i...")
            }
        }
    }
    companion object {
        @JvmStatic fun main(args: Array<String>) {
            CommandLine.run(MyApp(), System.err, *args)
        }
    }
}

Scala

Scala does not allow specifying array annotation attribute as a single value, so be aware that you will have to write Array(...) for the names, description and type attributes.

@Command(name = "MyApp", version = Array("Scala picocli demo v1.0"),
  description = Array("@|bold Scala|@ @|underline picocli|@ example"))
class MyApp extends Runnable {

  @Option(names = Array("-c", "--count"), paramLabel = "COUNT",
    description = Array("the count"))
  private val count: Int = 0

  @Option(names = Array("-h", "--help"), usageHelp = true,
    description = Array("print this help and exit"))
  private val helpRequested: Boolean = false

  @Option(names = Array("-V", "--version"), versionHelp = true,
    description = Array("print version info and exit"))
  private val versionRequested: Boolean = false

  def run() : Unit = {
    if (helpRequested) {
      new CommandLine(this).usage(System.err)
    } else if (versionRequested) {
      new CommandLine(this).printVersionHelp(System.err)
    } else {
      for (i <- 0 until count) {
        println(s"hello world $i...")
      }
    }
  }
}
object MyApp {
  def main(args: Array[String]) {
    CommandLine.run(new MyApp(), System.err, args: _*)
  }
}

Groovy

In Groovy, use [ and ] to surround array values, instead of the { and } used in Java.

class Args {
    @Option(names = ["-h", "--help"], usageHelp=true, 
                   description="Print this help and exit")
    boolean helpRequested = false;
}

I note that Kotlin 1.2 (still in beta) lifts the requirement of arrayOf for multiply-valued annotation parameters, using standard array notation instead (param = [value1, value2]):

https://blog.jetbrains.com/kotlin/2017/06/early-access-program-for-kotlin-1-2-has-been-started/

@binkley Thank you very much for letting me know! I've just started to update the manual for the upcoming picocli 2.0 release, so this is very timely!

Closing this ticket for the picocli-2.0 release.
(I may open another one for additional JVM language examples.)

Was this page helpful?
0 / 5 - 0 ratings