Skip to content

Commit

Permalink
[#454] doc: added kotlin script example
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jan 30, 2020
1 parent 3c85d4c commit 4969e8f
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env kscript

//DEPS info.picocli:picocli:4.2.0-SNAPSHOT
//@file:DependsOn("info.picocli:picocli:4.2.0-SNAPSHOT")
//import DependsOn

import java.util.concurrent.Callable

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

@Command(name="MainCmd",
subcommandsRepeatable = true,
subcommands = [Container::class])
class MainCmd : Callable<Int> {
@Option(names=["--help", "-h"], usageHelp=true)
var helpRequested: Boolean = false

override fun call(): Int {
println(this)
return 0
}
}

@Command(name="--with-container")
class Container : Callable<Int> {
@Parameters(arity="1")
lateinit var path: String

@Option(names=["--dataset", "-d"], arity="*")
var datasets: Array<String>? = null

@Option(names=["--help", "-h"], usageHelp=true)
var helpRequested: Boolean = false

override fun call(): Int {
println(this)
return 0
}

override fun toString() = ContainerData(path, datasets?.toList()).toString()
}

data class ContainerData(val path: String, val datasets: List<String>?)

val mainCmd = MainCmd()
val cl = CommandLine(mainCmd)
val exitCode = cl.execute(*args)

// example invocation:
// ./repeatable-subcmds-example.kts --with-container abc --dataset a --with-container xyz --dataset x

0 comments on commit 4969e8f

Please sign in to comment.