CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-groovysh

Interactive command-line shell (REPL) for evaluating Groovy expressions with advanced editing, completion, and command features

Pending
Overview
Eval results
Files

command-system.mddocs/

Command System

The Groovy Shell command system provides an extensible framework for implementing interactive shell commands. The system includes a command interface, registry management, and 15+ built-in commands for common shell operations.

Capabilities

Command Interface

Core interface that all shell commands must implement.

/**
 * Interface defining the contract for shell command implementations
 */
interface Command {
    /** Get the command's canonical name */
    String getName()
    
    /** Get the command's shortcut (typically single character) */
    String getShortcut()
    
    /** Get tab completion support for this command */
    Completer getCompleter()
    
    /** Get brief description of the command */
    String getDescription()
    
    /** Get usage information showing command syntax */
    String getUsage()
    
    /** Get detailed help text explaining the command */
    String getHelp()
    
    /** Get list of command aliases */
    List getAliases()
    
    /** Execute the command with provided arguments */
    Object execute(List<String> args)
    
    /** Check if command should be hidden from help listings */
    boolean getHidden()
}

Command Support Base Class

Abstract base class providing common functionality for command implementations.

/**
 * Abstract base class providing common functionality for command implementations
 */
abstract class CommandSupport implements Command {
    /** System line separator constant */
    protected static final String NEWLINE = System.lineSeparator()
    
    /** Command's canonical name */
    final String name
    
    /** Command's shortcut */
    final String shortcut
    
    /** List of command aliases */
    final List aliases
    
    /** Flag indicating if command is hidden from help */
    boolean hidden
    
    /**
     * Create command with name and shortcut
     * @param shell - The shell instance this command belongs to
     * @param name - Command's canonical name
     * @param shortcut - Command's shortcut
     */
    CommandSupport(Groovysh shell, String name, String shortcut)
}

Protected Methods (available to subclasses):

/** Override to provide custom tab completion */
List<Completer> createCompleters()

/** Create command alias */
void alias(String name, String shortcut)

/** Throw CommandException with message */
void fail(String msg)

/** Throw CommandException with cause */
void fail(String msg, Throwable cause)

/** Validate no arguments provided */
void assertNoArguments(List<String> args)

/** Access shell buffer manager */
BufferManager getBuffers()

/** Get current buffer */
List<String> getBuffer()

/** Get shell import statements */
List<String> getImports()

/** Get shell binding context */
Binding getBinding()

/** Get shell variables */
Map getVariables()

/** Get command history */
FileHistory getHistory()

/** Get shell class loader */
GroovyClassLoader getClassLoader()

Usage Example:

import org.apache.groovy.groovysh.CommandSupport

class CustomCommand extends CommandSupport {
    CustomCommand(shell) {
        super(shell, ':custom', ':c')
        this.description = 'Execute custom functionality'
    }
    
    Object execute(List<String> args) {
        if (args.size() == 0) {
            fail("Usage: :custom <argument>")
        }
        
        shell.io.out.println("Processing: ${args[0]}")
        return "Custom result: ${args[0]}"
    }
    
    List<Completer> createCompleters() {
        return [new SimpleCompleter('option1', 'option2', 'option3')]
    }
}

// Register the command
shell.register(new CustomCommand(shell))

Command Registry

Registry that manages available shell commands with name and shortcut uniqueness validation.

/**
 * Registry managing available shell commands with name/shortcut uniqueness
 */
class CommandRegistry {
    /** List of registered commands */
    final List<Command> commandList
    
    /**
     * Register a new command in the registry
     * @param command - Command to register
     * @return The registered command
     * @throws AssertionError if name or shortcut conflicts
     */
    Command register(Command command)
    
    /**
     * Find command by name or shortcut
     * @param name - Command name or shortcut to search for
     * @return Command instance or null if not found
     */
    Command find(String name)
    
    /**
     * Remove command from registry
     * @param command - Command to remove
     */
    void remove(Command command)
    
    /**
     * Get all registered commands
     * @return List of all commands
     */
    List<Command> commands()
    
    /**
     * Property access for commands by name
     * @param name - Command name
     * @return Command instance
     */
    Command getProperty(String name)
    
    /**
     * Iterator support for registry
     * @return Iterator over commands
     */
    Iterator iterator()
}

Command Alias

Represents a command alias that delegates execution to another command.

/**
 * Command alias that delegates to another command
 */
class CommandAlias implements Command {
    /** Target command this alias points to */
    final Command target
    
    /** Alias name */
    final String name
    
    /**
     * Create command alias
     * @param target - Command to alias
     * @param name - Alias name
     */
    CommandAlias(Command target, String name)
}

Built-in Commands

The shell provides 15+ built-in commands organized by functionality:

Core Commands

Essential shell operations and navigation.

class HelpCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':help'
    // Display help information for commands
}

class ExitCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':exit'
    // Exit the shell with optional exit code
}

class ClearCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':clear'
    // Clear current buffer contents
}

class DisplayCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':display'
    // Display current buffer contents
}

class ShowCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':show'
    // Show various shell information (variables, classes, imports, etc.)
}

File Operations

Commands for loading, saving, and editing files.

class LoadCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':load'
    // Load and execute Groovy scripts from files
}

class SaveCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':save'
    // Save current buffer contents to file
}

class EditCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':edit'
    // Edit buffer contents with external editor
}

Development Commands

Commands for code inspection, documentation, and dependency management.

class InspectCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':inspect'
    // Inspect objects and classes for methods and properties
}

class DocCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':doc'
    // Show documentation for classes and methods
}

class ImportCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':import'
    // Manage import statements
}

class GrabCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':grab'
    // Download and add dependencies using Grape
}

Utility Commands

Commands for shell configuration and session management.

class HistoryCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':history'
    // Manage and display command history
}

class AliasCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':alias'
    // Create and manage command aliases
}

class SetCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':set'
    // Configure shell settings and preferences
}

class RegisterCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':register'
    // Register new commands from classes or scripts
}

class RecordCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':record'
    // Record session activity to file
}

class PurgeCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':purge'
    // Purge classes from memory to free up space
}

class ShadowCommand extends CommandSupport {
    public static final String COMMAND_NAME = ':shadow'
    // Create shadow copies of classes for testing
}

Usage Examples:

// Load and execute a script
shell.execute(':load /path/to/script.groovy')

// Show all variables
shell.execute(':show variables')

// Inspect an object
shell.execute(':inspect myObject')

// Set shell preferences
shell.execute(':set interpreterMode true')

// Add import statement  
shell.execute(':import java.util.concurrent.*')

// Display command history
shell.execute(':history show')

// Create command alias
shell.execute(':alias :q :exit')

Command Registration Utilities

Helper classes for registering default and custom commands.

/**
 * Registers default shell commands
 */
class DefaultCommandsRegistrar {
    /**
     * Register all default commands with the shell
     */
    void register()
    
    /**
     * Create registrar for shell
     * @param shell - Shell instance to register commands with
     */
    DefaultCommandsRegistrar(Groovysh shell)
}

/**
 * Registers commands from XML configuration
 */
class XmlCommandRegistrar {
    /**
     * Register commands defined in XML resource
     * @param xmlResource - URL to XML configuration file
     */
    void register(URL xmlResource)
    
    /**
     * Create XML command registrar
     * @param shell - Shell instance
     * @param classLoader - ClassLoader for loading command classes
     */
    XmlCommandRegistrar(Groovysh shell, ClassLoader classLoader)
}

Exception Handling

/**
 * Exception thrown by command execution
 */
class CommandException extends Exception {
    /** Command that threw the exception */
    final Command command
    
    /**
     * Create command exception with message
     * @param command - Command that failed
     * @param message - Error message
     */
    CommandException(Command command, String message)
    
    /**
     * Create command exception with cause
     * @param command - Command that failed  
     * @param message - Error message
     * @param cause - Underlying exception
     */
    CommandException(Command command, String message, Throwable cause)
}

Command Argument Processing

/**
 * Utility for parsing command line arguments
 */
class CommandArgumentParser {
    /**
     * Parse command line into arguments
     * @param line - Command line string
     * @return List of parsed arguments
     */
    static List<String> parseLine(String line)
    
    /**
     * Parse command line with token limit
     * @param line - Command line string  
     * @param maxTokens - Maximum number of tokens to parse
     * @return List of parsed arguments
     */
    static List<String> parseLine(String line, int maxTokens)
}

The command system provides a flexible framework for extending the shell with custom functionality while maintaining consistency in argument processing, error handling, and completion support.

Install with Tessl CLI

npx tessl i tessl/maven-org-codehaus-groovy--groovy-groovysh

docs

command-system.md

completion-system.md

index.md

shell-features.md

utilities.md

tile.json