dependencies {// Required for all platforms implementation 'io.github.revxrsal:lamp.common:[VERSION]'// CLI module implementation 'io.github.revxrsal:lamp.cli:[VERSION]'}
dependencies {// Required for all platformsimplementation("io.github.revxrsal:lamp.common:[VERSION]")// CLI moduleimplementation("io.github.revxrsal:lamp.cli:[VERSION]")}
Optional: Preserve parameter names
Lamp identifies parameters by their names and uses them to generate relevant command metadata. By default, Java does not preserve parameter names reflectively. You need to add the following to your project:
compileJava { // Preserve parameter names in the bytecode options.compilerArgs += ["-parameters"]}// optional: if you're using KotlincompileKotlin { kotlinOptions.javaParameters =true}
tasks.withType<JavaCompile> {// Preserve parameter names in the bytecode options.compilerArgs.add("-parameters")}// optional: if you're using Kotlintasks.withType<KotlinJvmCompile> {compilerOptions { javaParameters =true }}
Supported CLI types
java.util.Scanner in place of CommandActor
java.io.PrintStream in place of CommandActor
Example
Execute and exit
publicfinalclassCLIApp {publicstaticvoidmain(String[] args) {var lamp =CLILamp.builder().build();lamp.register(newPingCommand());/* register all other commands here */ConsoleActor actor =ActorFactory.defaultFactory().createForStdIo(lamp);lamp.dispatch(actor,String.join(" ", args)); }staticclassPingCommand { @Command("ping")publicvoidping(ConsoleActor actor) {actor.reply("Pong!"); } }}
funmain(args: Array<String>) {val lamp = CLILamp.builder().build() lamp.register(PingCommand())/* register all other commands here */val actor = ActorFactory.defaultFactory().createForStdIo(lamp) lamp.dispatch(actor, args.joinToString(" "))}classPingCommand {@Command("ping")funping(actor: ConsoleActor) { actor.reply("Pong!") }}
Poll console indefinitely
publicfinalclassCLIApp {publicstaticvoidmain(String[] args) {var lamp =CLILamp.builder().build();lamp.register(newPingCommand());/* register all other commands here */// Call this to continuously read input from the console//// pollStdin() is CLIVisitors.pollStdin().// we use static imports here for brevity.lamp.accept(pollStdin()); }staticclassPingCommand { @Command("ping")publicvoidping(ConsoleActor actor) {actor.reply("Pong!"); } }}
funmain(args: Array<String>) {val lamp = CLILamp.builder().build() lamp.register(PingCommand())/* register all other commands here */// Call this to continuously read input from the console//// pollStdin() is CLIVisitors.pollStdin().// we use static imports here for brevity. lamp.accept(pollStdin())}classPingCommand {@Command("ping")funping(actor: ConsoleActor) { actor.reply("Pong!") }}