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

Looking for examples of using picocli from other JVM languages #183

Closed
remkop opened this issue Sep 12, 2017 · 4 comments
Closed

Looking for examples of using picocli from other JVM languages #183

remkop opened this issue Sep 12, 2017 · 4 comments

Comments

@remkop
Copy link
Owner

remkop commented Sep 12, 2017

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.


@remkop
Copy link
Owner Author

remkop commented Sep 12, 2017

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;
}

@remkop remkop added this to the 2.0.0 milestone Oct 7, 2017
@binkley
Copy link

binkley commented Oct 8, 2017

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/

@remkop
Copy link
Owner Author

remkop commented Oct 9, 2017

@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!

remkop added a commit that referenced this issue Oct 9, 2017
remkop added a commit that referenced this issue Oct 9, 2017
remkop added a commit that referenced this issue Oct 9, 2017
@remkop remkop changed the title Add examples to user manual for using picocli in other JVM languages Examples of using picocli to parse the command line in other JVM languages Oct 10, 2017
@remkop remkop changed the title Examples of using picocli to parse the command line in other JVM languages Examples of using picocli from other JVM languages to parse command line arguments Oct 10, 2017
@remkop remkop changed the title Examples of using picocli from other JVM languages to parse command line arguments Looking for examples of using picocli from other JVM languages to parse command line arguments Oct 10, 2017
@remkop remkop changed the title Looking for examples of using picocli from other JVM languages to parse command line arguments Looking for examples of using picocli from other JVM languages Oct 10, 2017
@remkop
Copy link
Owner Author

remkop commented Oct 21, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants