|
Knopflerfish OSGi 2.4.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.knopflerfish.service.console.CommandGroupAdapter
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}
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; } }
Field Summary | |
static java.lang.String |
COMMAND_GROUP
Full class name of CommandGroup interface. |
Fields inherited from interface org.knopflerfish.service.console.CommandGroup |
GROUP_NAME |
Constructor Summary | |
CommandGroupAdapter(java.lang.String groupName,
java.lang.String shortHelp)
Constructs a command group. |
Method Summary | |
int |
execute(java.lang.String[] args,
java.io.Reader in,
java.io.PrintWriter out,
Session session)
Executes a command in the command group. |
java.lang.String |
getGroupName()
Returns the command group name. |
java.lang.String |
getLongHelp()
Returns long command group help. |
java.util.Dictionary |
getOpt(java.lang.String[] args,
java.lang.String usage)
Method to do argument parsing. |
java.lang.String |
getShortHelp()
Returns short command group help. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String COMMAND_GROUP
Constructor Detail |
public CommandGroupAdapter(java.lang.String groupName, java.lang.String shortHelp)
groupName
- the name for this command groupshortHelp
- one line description of this command groupMethod Detail |
public java.lang.String getGroupName()
getGroupName
in interface CommandGroup
public java.lang.String getShortHelp()
getShortHelp
in interface CommandGroup
public java.lang.String getLongHelp()
HELP_{CMD}
and USAGE_{CMD}
variables of the
sub-class.
getLongHelp
in interface CommandGroup
public 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 CommandGroup
args
- argument list passed to the commandout
- output device to print resultin
- input for commandsession
- a handle to command session or null if single command
public java.util.Dictionary getOpt(java.lang.String[] args, java.lang.String usage) throws java.lang.Exception
args
- argument list passed to the commandusage
- usage string
java.lang.Exception
- Thrown if it fails to parse args or usage
|
Knopflerfish OSGi 2.4.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |