-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#454] doc: added kotlin script example
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
.../main/kotlin/picocli/examples/kotlin/repeatablesubcommands/repeatable-subcmds-example.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |