com.rathravane.till.console.shell.stdCommandList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of silt Show documentation
Show all versions of silt Show documentation
A small collection of classes used in various Rathravane systems.
/*
* Copyright 2006-2012, Rathravane LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rathravane.till.console.shell;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import com.rathravane.till.console.CmdLineParser;
import com.rathravane.till.console.CmdLinePrefs;
public class stdCommandList implements commandList
{
public stdCommandList ()
{
this ( true );
}
public stdCommandList ( boolean withStdCommands )
{
fCommands = new HashMap ();
if ( withStdCommands )
{
addStandardCommands ();
}
}
public void registerCommand ( command c )
{
fCommands.put ( c.getCommand (), c );
}
public void removeCommand ( String key )
{
fCommands.remove ( key );
}
public void clearCommands ()
{
fCommands.clear ();
}
public void addStandardCommands ()
{
registerCommand ( new simpleCommand ( "exit" )
{
@Override
public consoleLooper.inResult execute ( HashMap workspace, CmdLinePrefs prefs, PrintStream outTo ) { return consoleLooper.inResult.kQuit; }
} );
registerCommand ( new simpleCommand ( "quit" )
{
@Override
public consoleLooper.inResult execute ( HashMap workspace, CmdLinePrefs prefs, PrintStream outTo ) { return consoleLooper.inResult.kQuit; }
} );
registerCommand ( new simpleCommand ( "help", "help []" )
{
@Override
protected void setupParser ( CmdLineParser clp )
{
clp.requireFileArguments ( 0, 1 );
}
@Override
public consoleLooper.inResult execute ( HashMap workspace, CmdLinePrefs prefs, PrintStream outTo )
{
if ( prefs.getFileArguments ().size() == 1 )
{
final String cmdText = prefs.getFileArguments ().firstElement ();
final command cmd = fCommands.get ( cmdText );
if ( cmd != null )
{
outTo.println ( " " + cmd.getUsage () );
final String help = cmd.getHelp ();
if ( help != null )
{
outTo.println ();
outTo.println ( help );
}
}
else
{
outTo.println ( "Unknown command: " + cmdText + "." );
}
}
else
{
outTo.println ( "The available commands are:" );
outTo.println ();
final LinkedList commands = new LinkedList ();
commands.addAll ( getAllCommands () );
Collections.sort ( commands );
for ( String cmd : commands )
{
outTo.println ( " " + cmd );
}
outTo.println ();
outTo.println ( "You can type 'help ' to get more info on that command." );
}
return consoleLooper.inResult.kReady;
}
} );
}
public Collection getAllCommands ()
{
return fCommands.keySet ();
}
@Override
public command getCommandFor ( String cmd )
{
return fCommands.get ( cmd );
}
private final HashMap fCommands;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy