INTERFACE

tcl.lang.Command -- The interface for defining new Tcl commands.

METHODS

abstract void cmdProc(Interp interp, TclObject argv[])

ARGUMENTS

Interp interp ()
The interpreter in which the command is executed.

TclObject argv[] ()
The array of argument objects passed to the command.

DESCRIPTION

To write a new Tcl command in Java, you write a class that implement the Command interface and its cmdProc method. Then, register the command using the interp.createCommand method.

cmdProc is invoked to process a Tcl command for interp. The argv array contains the arguments passed to the command: argv[0] is the name of the command and the rest of the array contains the rest of the arguments.

If the command execution completes normally, this method should pass the result object back to the interpreter by calling interp.setResult and then return normally. If an error occurs during the command execution, cmdProc should throw a TclException with appropriate completion code and error messages (see the manual entry of TclException for details.)

If your command requires clean-up when it's removed from the interpreter, use the CommandWithDispose interface instead of the Command interface. See the CommandWithDispose manual entry for details.

EXAMPLE

Here is an example of a Tcl command that adds two integers and returns the result.
import tcl.lang;

class AddCmd implements Command {
    public cmdProc(Interp interp, TclObject argv[])
            throws TclException {
        if (argv.length != 3) {
            throw new TclNumArgsException(interp, 1, argv,
                "num1 num2");
        }

        int num1 = TclInteger.get(interp, argv[1]);
        int num2 = TclInteger.get(interp, argv[2]);

        TclObject result =
            TclInteger.newInstance(num1 + num2);
        interp.setResult(result);
    }
}

EQUIVALENT C FUNCTIONS

Tcl_CommandProc

SEE ALSO

Interp, TclException, TclInteger, TclNumArgsException, TclObject, CommandWithDispose

KEYWORDS

command, create, interpreter, exception
Copyright © 1998 Sun Microsystems, Inc.
Copyright © 1995-1997 Roger E. Critchlow Jr.