Previous Topic

Next Topic

Book Contents

Book Index

Basics for Annotated Commands

To develop console commands in more convenient and safer manner than with PluggableCommands, use AnnotatedCommands abstract class and its supplementary annotations.

Register Annotated commands either:

Analogically, unregister them using:

The following specifics should be noted when creating AnnotatedCommands:

Here is an example of a command that prints “Hello World!”, followed by its two arguments:

and then its two options:

@Command(

aliases = {

      "hw", "hello"

  },

name = "helloworld",

description = "Prints hello world!")

public void helloworld(

@Argument(description = "first arg", required = true) String arg1,

@Argument(description = "second arg", required = false) char[] arg2,

@Option(def = "123", description = "first option", name = "o") String opt1,

@Option(def = {

          "1", "3"

}, description = "second option", name = "i") int[] opt2) {

    println("Hello World!");

    println("Arguments:");

    println("Arg1:" + arg1);

    print("Arg2:");

    for (char arg : arg2) {

        print(arg + ", ");

    }

    println("");

    println("Options:");

    println("opt1:" + opt1);

    print("opt2:");

    for (int i : opt2) {

        print(i + ", ");

    }

}

The invocation of the command in the runtime is as follows (assuming ‘groupname’ is our used group name from within the AnnotatedCommands extension constructor) :

groupname>$helloworld String1 a b c -o String2 -i 2 -i 3 -i 4

Hello World!

Arguments:

Arg1:String1

Arg2:a, b, c ,

Options:

opt1:String2

opt2:2, 3, 4,

The aliases can also be used to call the command – instead of “helloworld” we could have written “hw” and “hello”.

Here we have an example of arguments and options which are collections. How to specify them is described in Supplying Arrays and Collections as Options.