This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Yevgeny Krasik edited this page Jan 10, 2016
·
7 revisions
Java Annotation Command Interface.
Jaci generates User Interfaces (CLI/GUI) from annotated methods, which can then be embedded into your application.
The library tries to be as UI-platform independent as possible, only requiring a thin integration layer for each UI-platform.
Jaci is geared towards 2 things:
- Creating debug consoles - A debug console doesn't need to look pretty, it needs to be convenient to work with (at runtime) and convenient to add commands to (at dev time).
- Creating quick, throw-away prototype code - When you just need to quickly test some logic (or learn a new API), just write the logic, add annotations and let Jaci generate the UI.
Jaci allows the developer to focus on what's important - the logic.
The developer writes the logic, provides metadata about the logic (annotations) and lets Jaci generate a UI that can call this logic.
- Command - A piece of logic that may receive parameters.
- Directory - A group of related commands. Directories may contain child directories and commands.
- Command Hierarchy - A hierarchy of directories and commands, starting from a root directory.
- Path - A '/' delimited string that points to an entry in the hierarchy.
- Output - An output source for commands to use. Can display normal and error messages.
@CommandPath("example/simple")
public class Example {
private CommandOutput output;
@Command(description = "Does nothing, really.")
public void simpleCommand(@StringParam(value = "str1", accepts = {"a", "b", "c"}) String str1,
@StringParam(value = "str2", acceptsSupplier = "strSupplier", optional = true, defaultValue = "default") String str2,
@IntParam("int") int i,
@DoubleParam(value = "double", optional = true, defaultValueSupplier = "doubleSupplier") double d,
@BoolParam(value = "flag", optional = true) boolean flag) {
output.message("str1=" + str1 + ", str2=" + str2 + ", i=" + i + ", d=" + d + ", flag=" + flag);
}
}
private String[] strSupplier() {
return new String[] {"d", "e", "f"};
}
private double doubleSupplier() {
return 3.5;
}
Let's explain what's going on here:
- Example defines a directory that has 1 command and is mounted at: 'example/simple'.
- There is a private, non-initialized CommandOutput parameter. CommandOutput is the way by which commands send output to the host application. When processing a class, Jaci will inject any fields of the type CommandOutput with an implementing instance.
- The single command that is defined in the class, 'simpleCommand' has 5 parameters:
- A mandatory string called
str1
that only accepts the values {a
,b
,c
}. - An optional string called
str2
that only accepts the values {d
,e
,f
} (supplied by a supplier method). If the parameter doesn't receive a value, it will have a value ofdefault
. - A mandatory int called
int
. - An optional double called
double
. If the parameter doesn't receive a value, it will have a value of3.5
(supplied by a supplier method). - An optional boolean called
flag
.
- A mandatory string called
- The command then just sends the parameter values to the output. The string sent to the output will be formatted with String.format().
Here is how this looks on a LibGdx CLI implementation:
-
ls -r
- Print the contents of our hierarchy, recursing into sub-directories. -
cd example/simple
- Change working directory to toexample/simple
. -
simpleCommand a d 3 4.5 true
- Call simpleCommand with these values. - Some invocations of simpleCommand with invalid parameter values.
-
simpleCommand a d -int 3 -double 4.5 -flag
- Call simpleCommand with a mix of passing values by-position and by-name.-int 3
passes the value 3 for the parameter namedint
.-flag
is a shortcut only applicable to optional boolean parameters where just specifying the parameter name without a value afterwards will bind the boolean parameter to the inverse of it's default value. -
simpleCommand a -int 3 -double 4.5 -flag
- Call simpleCommand without specifying the value ofstr2
, it receives the default value of "default". -
simpleCommand a -int 3 -flag
- Call simpleCommand without specifying the value ofstr2
ordouble
, they receive the default values of "default" and 3.5, respectively. -
simpleCommand a -int 3
- Call simpleCommand with only values bound to mandatory parameters, all optional parameters receive default values. -
simpleCommand a 3
- A parse error, since the next parameter to parse a value according to it's position isstr2
.
Structure | Annotation API | CLI Information | Integration |
---|---|---|---|
Structure | Annotation API | CLI | LibGdx Integration |