All Packages Class Hierarchy This Package Previous Next Index
Class calypso.util.Shell
java.lang.Object
|
+----calypso.util.Shell
- public class Shell
- extends Object
- implements Runnable
A class for easy implementation of command line interfaces. The Shell
reads and parses command lines typed by the user (or read from any
input stream) and executes the given commands. Commands must be
pre-registered to the shell as ShellCommand implementations.
The shell is easily extended with inner classes:
...
Shell sh = new Shell();
sh.registerCommand("echo",
new ShellCommand("echoes all given parameters")
{
public void execute(StringTokenizer parameters_, Shell sh_)
{
while (parameters_.hasMoreTokens())
{
sh_.getOut().print(parameters_.nextToken() + " ");
}
sh_.getOut().print("\n");
}
});
sh.run();
- Version:
- $Reveision: $
- Author:
- Juhana Räsänen
- See Also:
- ShellCommand
-
DEF_CMDNAME
- The default command name.
-
DEF_PROMPT
- The default prompt.
-
Shell()
- Constructs a new shell that uses System.out and System.in as streams
and default prompt.
-
Shell(boolean)
- Constructs a new shell that uses System.out and System.in as streams
and default prompt with current time.
-
Shell(InputStream, PrintStream)
- Constructs a shell with given streams and the default prompt.
-
Shell(InputStream, PrintStream, String)
- Constructs a new shell with given streams and prompt.
-
Shell(String)
- Constructs a new shell with given prompt.
-
Shell(String, boolean)
- Constructs a new shell with given prompt and current time.
-
clone()
- Makes a shallow copy of the Shell.
-
getIn()
- Returns the input stream
-
getOut()
- Returns the output stream
-
main(String[])
- A main method for testing and executing simple scripts.
-
registerCommand(String, ShellCommand)
- Registers a new command to the shell.
-
run()
- The main loop of the shell.
-
setExitFlag(boolean)
- Sets the exit status flag.
-
setPrompt(String)
- Sets the prompt shown to the user.
-
unregisterCommand(String)
- Unregisters a command so that the shell does not recognize it anymore.
DEF_CMDNAME
public static final String DEF_CMDNAME
- The default command name. The default command is run when the user
gives a command that is not stored in the command table.
DEF_PROMPT
public static final String DEF_PROMPT
- The default prompt.
Shell
public Shell()
- Constructs a new shell that uses System.out and System.in as streams
and default prompt.
Shell
public Shell(boolean showTime_)
- Constructs a new shell that uses System.out and System.in as streams
and default prompt with current time.
- Parameters:
- showTime_ -
true
if current time is to be shown
Shell
public Shell(String prompt_)
- Constructs a new shell with given prompt. System.in and System.out
are used as the streams.
- Parameters:
- prompt_ - the user prompt
Shell
public Shell(String prompt_,
boolean showTime_)
- Constructs a new shell with given prompt and current time.
System.in and System.out are used as the streams.
- Parameters:
- prompt_ - the user prompt
- showTime_ -
true
if current time is to be shown
Shell
public Shell(InputStream is_,
PrintStream os_)
- Constructs a shell with given streams and the default prompt.
- Parameters:
- is_ - the input stream
- os_ - the output stream
Shell
public Shell(InputStream is_,
PrintStream os_,
String prompt_)
- Constructs a new shell with given streams and prompt. This constructor
is called eventually by the other constructors and it also creates the
default shell commands (as anonymous inner classes).
- Parameters:
- is_ - the input stream
- os_ - the output stream
- prompt_ - the user prompt
clone
public Object clone()
- Makes a shallow copy of the Shell. The command table is copied using
Hashtable.clone().
- Returns:
- The new copy of the Shell
- Overrides:
- clone in class Object
registerCommand
public void registerCommand(String cmdname_,
ShellCommand cmd_)
- Registers a new command to the shell. If there is an existing command
with the given name, the previous command is discarded.
- Parameters:
- cmdname_ - the name of the command (must be a signle word)
- cmd_ - the actual command implementation
unregisterCommand
public void unregisterCommand(String cmdname_)
- Unregisters a command so that the shell does not recognize it anymore.
- Parameters:
- cmdname_ - the name of the command
setExitFlag
public void setExitFlag(boolean value_)
- Sets the exit status flag. If set as true, the shell command loop
will exit the next round
- Parameters:
- value_ - true or false
setPrompt
public void setPrompt(String prompt_)
- Sets the prompt shown to the user.
- Parameters:
- prompt_ - the new prompt string
getOut
public PrintStream getOut()
- Returns the output stream
- Returns:
- the output stream
getIn
public InputStream getIn()
- Returns the input stream
- Returns:
- the input stream
run
public void run()
- The main loop of the shell. Executes until the exitFlag becomes true.
Prints user the command prompt, reads a new command line from the
input stream, parses it and executes the command if found.
main
public static void main(String argv[])
- A main method for testing and executing simple scripts.
- Parameters:
- command - line parameters (not used)
All Packages Class Hierarchy This Package Previous Next Index