org.knopflerfish.service.console
public abstract class CommandGroupAdapter extends java.lang.Object implements CommandGroup
To create your own command group you extend this class and add two variables and a method.
USAGE_{NAME}
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}int cmd{Name}(Dictionary, Reader, PrintWriter, Session)
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;
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
CommandGroupAdapter.DynamicCmd |
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
COMMAND_GROUP
Full class name of CommandGroup interface.
|
GROUP_NAME| Constructor and Description |
|---|
CommandGroupAdapter(java.lang.String groupName,
java.lang.String shortHelp)
Constructs a command group.
|
| Modifier and Type | Method and Description |
|---|---|
int |
execute(java.lang.String[] args,
java.io.Reader in,
java.io.PrintWriter out,
Session session)
Executes a command in the command group.
|
java.util.Map<java.lang.String,java.lang.String> |
getCommandNames() |
java.lang.String |
getGroupName()
Returns the command group name.
|
java.lang.String |
getLongHelp()
Returns long command group help.
|
java.util.Dictionary<java.lang.String,java.lang.Object> |
getOpt(java.lang.String[] args,
java.lang.String usage)
Method to do argument parsing.
|
java.lang.String |
getShortHelp()
Returns short command group help.
|
public static final java.lang.String COMMAND_GROUP
public CommandGroupAdapter(java.lang.String groupName,
java.lang.String shortHelp)
groupName - the name for this command groupshortHelp - one line description of this command grouppublic java.lang.String getGroupName()
getGroupName in interface CommandGrouppublic java.lang.String getShortHelp()
getShortHelp in interface CommandGrouppublic java.lang.String getLongHelp()
HELP_{CMD} and USAGE_{CMD} variables of the
sub-class.getLongHelp in interface CommandGrouppublic int execute(java.lang.String[] args,
java.io.Reader in,
java.io.PrintWriter out,
Session session)
USAGE_{CMD} string of that command and calls its
cmd{Cmd} method of the sub-class.execute in interface CommandGroupargs - argument list passed to the commandout - output device to print resultin - input for commandsession - a handle to command session or null if single commandpublic java.util.Dictionary<java.lang.String,java.lang.Object> getOpt(java.lang.String[] args,
java.lang.String usage)
throws java.lang.Exception
args - argument list passed to the commandusage - usage stringjava.lang.Exception - Thrown if it fails to parse args or usagepublic java.util.Map<java.lang.String,java.lang.String> getCommandNames()