🛖
Fox Hut
  • Introduction
    • Setting up
    • CommandActor, @Command and @Subcommand
    • Creating your first command
    • Improving our greet command
  • Platforms
    • Bukkit / Spigot / Paper
    • BungeeCord
    • Velocity
    • Sponge
    • Fabric
    • Brigadier
    • Minestom
    • JDA
    • Command line
  • How-to
    • Creating variants of /teleport
    • Custom parameter types
    • Suggestions and auto-completion
    • Context parameters
    • Command permissions
    • Parameter validators
    • Command conditions
    • Response handlers
    • Cooldowns
    • Help commands
    • Annotation replacers
    • Orphan command
    • Exception handling
    • Hooks
    • Dependency injection
    • Visitors
    • Customizing the dispatcher and failure behavior
Powered by GitBook
On this page
  • Setting up
  • Adding Lamp dependency
  • Supported CLI types
  • Example
  • Execute and exit
  • Poll console indefinitely

Was this helpful?

Edit on GitHub
  1. Platforms

Command line

This page describes how to integrate Lamp with the command line, as well as what to expect out of it.

Setting up

Adding Lamp dependency

To add Lamp to your project, add the following (depending on your project structure):

<dependencies>
  <!-- Required for all platforms -->
  <dependency>
      <groupId>io.github.revxrsal</groupId>
      <artifactId>lamp.common</artifactId> 
      <version>[VERSION]</version>
  </dependency>

  <!-- CLI module -->
  <dependency>
      <groupId>io.github.revxrsal</groupId>
      <artifactId>lamp.cli</artifactId>
      <version>[VERSION]</version>
  </dependency>  
</dependencies>
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 platforms
    implementation("io.github.revxrsal:lamp.common:[VERSION]")
    
    // CLI module
    implementation("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:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <compilerArgs>
        <!-- Preserves parameter names -->
        <arg>-parameters</arg>
      </compilerArgs>
    </configuration>
  </plugin>
</plugins>
compileJava { 
    // Preserve parameter names in the bytecode
    options.compilerArgs += ["-parameters"]
}

// optional: if you're using Kotlin
compileKotlin {
    kotlinOptions.javaParameters = true
}
tasks.withType<JavaCompile> {
    // Preserve parameter names in the bytecode
    options.compilerArgs.add("-parameters")
}

// optional: if you're using Kotlin
tasks.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

public final class CLIApp {

    public static void main(String[] args) {
        var lamp = CLILamp.builder()
                .build();
        lamp.register(new PingCommand());
        /* register all other commands here */

        ConsoleActor actor = ActorFactory.defaultFactory().createForStdIo(lamp);
        lamp.dispatch(actor, String.join(" ", args));
    }

    static class PingCommand {

        @Command("ping")
        public void ping(ConsoleActor actor) {
            actor.reply("Pong!");
        }
    }
}
fun main(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(" "))
}

class PingCommand {

    @Command("ping")
    fun ping(actor: ConsoleActor) {
        actor.reply("Pong!")
    }
}

Poll console indefinitely

public final class CLIApp {

    public static void main(String[] args) {
        var lamp = CLILamp.builder()
                .build();
        lamp.register(new 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());
    }

    static class PingCommand {

        @Command("ping")
        public void ping(ConsoleActor actor) {
            actor.reply("Pong!");
        }
    }
}
fun main(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())
}

class PingCommand {

    @Command("ping")
    fun ping(actor: ConsoleActor) {
        actor.reply("Pong!")
    }
}
PreviousJDANextCreating variants of /teleport

Last updated 4 months ago

Was this helpful?

Latest version: