Class DefaultCommandDispatcher

  • All Implemented Interfaces:
    java.lang.AutoCloseable, CommandDispatcher

    public class DefaultCommandDispatcher
    extends java.lang.Object
    implements CommandDispatcher
    Default implementation of CommandDispatcher that supports pipeline execution.

    This dispatcher parses command lines using PipelineParser, resolves commands from registered CommandGroups, and executes pipeline stages with proper pipe/redirect/conditional handling.

    When a JobManager is configured, the dispatcher supports:

    • Background execution via trailing &
    • Foreground job tracking
    • Built-in jobs, fg, bg commands

    When an AliasManager is configured, the dispatcher expands aliases before pipeline parsing and automatically registers alias/unalias commands.

    Since:
    4.0
    See Also:
    CommandDispatcher, PipelineParser
    • Constructor Detail

      • DefaultCommandDispatcher

        public DefaultCommandDispatcher​(Terminal terminal)
        Creates a new dispatcher for the given terminal.
        Parameters:
        terminal - the terminal
      • DefaultCommandDispatcher

        public DefaultCommandDispatcher​(LineReader reader)
        Creates a new dispatcher for the given reader.
        Parameters:
        reader - the reader
      • DefaultCommandDispatcher

        public DefaultCommandDispatcher​(Terminal terminal,
                                        JobManager jobManager)
        Creates a new dispatcher for the given terminal with optional job management.
        Parameters:
        terminal - the terminal
        jobManager - the job manager, or null for no job control
      • DefaultCommandDispatcher

        public DefaultCommandDispatcher​(Terminal terminal,
                                        JobManager jobManager,
                                        PipelineParser pipelineParser,
                                        AliasManager aliasManager)
        Creates a new dispatcher with all configuration options.

        When a non-null JobManager is provided, the dispatcher automatically registers JobCommands and supports background execution via trailing &.

        When a non-null AliasManager is provided, the dispatcher expands aliases before pipeline parsing and automatically registers AliasCommands.

        When a non-null PipelineParser is provided, it is used instead of the default parser. This allows custom operator registration and subclass overrides.

        Parameters:
        terminal - the terminal
        jobManager - the job manager, or null for no job control
        pipelineParser - the pipeline parser, or null for the default parser
        aliasManager - the alias manager, or null for no alias support
      • DefaultCommandDispatcher

        public DefaultCommandDispatcher​(Terminal terminal,
                                        JobManager jobManager,
                                        PipelineParser pipelineParser,
                                        AliasManager aliasManager,
                                        LineExpander lineExpander,
                                        ScriptRunner scriptRunner)
        Creates a new dispatcher with all configuration options including line expansion and script execution.
        Parameters:
        terminal - the terminal
        jobManager - the job manager, or null for no job control
        pipelineParser - the pipeline parser, or null for the default parser
        aliasManager - the alias manager, or null for no alias support
        lineExpander - the line expander, or null for no variable expansion
        scriptRunner - the script runner, or null for no script support
      • DefaultCommandDispatcher

        public DefaultCommandDispatcher​(Terminal terminal,
                                        JobManager jobManager,
                                        PipelineParser pipelineParser,
                                        AliasManager aliasManager,
                                        LineExpander lineExpander,
                                        ScriptRunner scriptRunner,
                                        Parser parser)
        Creates a new dispatcher with all configuration options including line expansion, script execution and parser.
        Parameters:
        terminal - the terminal
        jobManager - the job manager, or null for no job control
        pipelineParser - the pipeline parser, or null for the default parser
        aliasManager - the alias manager, or null for no alias support
        lineExpander - the line expander, or null for no variable expansion
        scriptRunner - the script runner, or null for no script support
        parser - the argument parser, or null for the default parser
    • Method Detail

      • execute

        public java.lang.Object execute​(java.lang.String line)
                                 throws java.lang.Exception
        Description copied from interface: CommandDispatcher
        Executes a command line string.

        The dispatcher parses the line (potentially into a pipeline) and executes the commands.

        Specified by:
        execute in interface CommandDispatcher
        Parameters:
        line - the command line to execute
        Returns:
        the result of execution
        Throws:
        java.lang.Exception - if execution fails
      • execute

        public java.lang.Object execute​(Pipeline pipeline)
                                 throws java.lang.Exception
        Description copied from interface: CommandDispatcher
        Executes a pre-parsed pipeline.
        Specified by:
        execute in interface CommandDispatcher
        Parameters:
        pipeline - the pipeline to execute
        Returns:
        the result of execution
        Throws:
        java.lang.Exception - if execution fails
      • completer

        public Completer completer()
        Creates a completer that provides completions for every registered command name and its aliases. Builds a SystemCompleter, registers a completer for each command and each of its alias strings, and finalizes it into a candidate-producing completer.
        Specified by:
        completer in interface CommandDispatcher
        Returns:
        a Completer that offers argument and command-name completions for all registered commands and aliases
      • describe

        public CommandDescription describe​(CommandLine commandLine)
        Description copied from interface: CommandDispatcher
        Returns a description for the command at the current command line position.

        Used by widgets for context-sensitive help.

        Specified by:
        describe in interface CommandDispatcher
        Parameters:
        commandLine - the parsed command line
        Returns:
        the command description, or null
      • session

        public CommandSession session()
        Returns the command session used by this dispatcher.
        Returns:
        the command session
      • interruptCurrentCommand

        public void interruptCurrentCommand()
        Interrupts the currently executing command, if any.

        This is typically called from a signal handler (e.g., SIGINT) to interrupt the foreground command without killing the shell.

      • initialize

        public void initialize​(java.io.File script)
                        throws java.lang.Exception
        Description copied from interface: CommandDispatcher
        Initializes the dispatcher, optionally executing a startup script.
        Specified by:
        initialize in interface CommandDispatcher
        Parameters:
        script - the initialization script, or null
        Throws:
        java.lang.Exception - if initialization fails
      • close

        public void close()
        No-op default implementation; does nothing. Subclasses may override to release or close allocated resources.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface CommandDispatcher