Adapter class for CommandGroup. Simplifies the creations of command groups. It takes away the work of parsing command lines and generates help text. It uses reflection to check which commands you have created.
To create your own command group you extend this class and add two variables and a method.
USAGE_{NAME}
- A static String (NAME must be capital letters) that describes the command line options of the command. The following is valid usage string (only space can be used as whitespace):
usage : [ flags ] [ args [ '...' ]] flags : flag [ flags ] flags : oflag [ flags ] flag : flag '|' flag flag : '-'FLAGNAME [ '#'[TEXT['#']] ] oflag : '[' flag ']' args : '<' ARGNAME '>' args : '[' args ']'
Note: The flag "-help" is automatically added and handled by CommandGroupAdapter. HELP_{NAME}
- A static String array (NAME must be capital letters) that gives the help text for the command. Each element is printed on its own line. The first element should be a short description of the command as it is used to describe the command when we generate the help text for the command group.
int cmd{Name}(Dictionary, Reader, PrintWriter, Session)
- A method (the first letter in the command name must be capital, and the rest must be lowercase) that is called when the CommandGroupAdapter has matched the command and decode the command flags. The Dictionary contains the parsed commands arguments. If a flag is present the key "-FLAGNAME" is present and any value as the object (type String) connected to the key. The same goes for "ARGNAME". If the usage string ends with "...", then the last ARGNAME key is connected with a String array object that contains all the remaining arguments on the command line. The method parameters Reader, PrintWriter and Session are the same as for the execute method. The method should return a 0 if the command executed okey.
The object must then be registered under the class name
org.knopflerfish.service.console.CommandGroup
with the property "groupName" set to the command group name.
Example:
package com.apa; import java.io.*; import java.util.*; import org.knopflerfish.service.console.*; public class MyCommandGroup extends CommandGroupAdapter { MyCommandGroup() { super("echocommands", "Echo commands"); } public final static String USAGE_ECHO = "[-n] <text> ..."; public final static String[] HELP_ECHO = new String[] { "Echo command arguments", "-n Don't add newline at end", "<text> Text to echo" }; public int cmdEcho(Dictionary opts, Reader in, PrintWriter out, Session session) { String[] t = (String[]) opts.get("text"); for (int i = 0; i < t.length; i++) { out.print(t[i]); } if (opts.get("-n") == null) { out.println(); } return 0; } }
@author Gatespace AB