Class DefaultCommandDispatcher
- java.lang.Object
-
- org.jline.shell.impl.DefaultCommandDispatcher
-
- All Implemented Interfaces:
java.lang.AutoCloseable,CommandDispatcher
public class DefaultCommandDispatcher extends java.lang.Object implements CommandDispatcher
Default implementation ofCommandDispatcherthat supports pipeline execution.This dispatcher parses command lines using
PipelineParser, resolves commands from registeredCommandGroups, and executes pipeline stages with proper pipe/redirect/conditional handling.When a
JobManageris configured, the dispatcher supports:- Background execution via trailing
& - Foreground job tracking
- Built-in
jobs,fg,bgcommands
When an
AliasManageris configured, the dispatcher expands aliases before pipeline parsing and automatically registersalias/unaliascommands.- Since:
- 4.0
- See Also:
CommandDispatcher,PipelineParser
-
-
Constructor Summary
Constructors Constructor Description DefaultCommandDispatcher(LineReader reader)Creates a new dispatcher for the given reader.DefaultCommandDispatcher(Terminal terminal)Creates a new dispatcher for the given terminal.DefaultCommandDispatcher(Terminal terminal, JobManager jobManager)Creates a new dispatcher for the given terminal with optional job management.DefaultCommandDispatcher(Terminal terminal, JobManager jobManager, PipelineParser pipelineParser, AliasManager aliasManager)Creates a new dispatcher with all configuration options.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.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddGroup(CommandGroup group)Adds a command group to this dispatcher.voidclose()No-op default implementation; does nothing.Completercompleter()Creates a completer that provides completions for every registered command name and its aliases.CommandDescriptiondescribe(CommandLine commandLine)Returns a description for the command at the current command line position.java.lang.Objectexecute(java.lang.String line)Executes a command line string.java.lang.Objectexecute(Pipeline pipeline)Executes a pre-parsed pipeline.java.util.List<CommandGroup>groups()Returns all registered command groups.voidinitialize(java.io.File script)Initializes the dispatcher, optionally executing a startup script.voidinterruptCurrentCommand()Interrupts the currently executing command, if any.CommandSessionsession()Returns the command session used by this dispatcher.Terminalterminal()Returns the terminal associated with this dispatcher.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jline.shell.CommandDispatcher
cleanUp, errorStyle, findCommand, trace
-
-
-
-
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 terminaljobManager- 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
JobManageris provided, the dispatcher automatically registersJobCommandsand supports background execution via trailing&.When a non-null
AliasManageris provided, the dispatcher expands aliases before pipeline parsing and automatically registersAliasCommands.When a non-null
PipelineParseris provided, it is used instead of the default parser. This allows custom operator registration and subclass overrides.- Parameters:
terminal- the terminaljobManager- the job manager, or null for no job controlpipelineParser- the pipeline parser, or null for the default parseraliasManager- 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 terminaljobManager- the job manager, or null for no job controlpipelineParser- the pipeline parser, or null for the default parseraliasManager- the alias manager, or null for no alias supportlineExpander- the line expander, or null for no variable expansionscriptRunner- 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 terminaljobManager- the job manager, or null for no job controlpipelineParser- the pipeline parser, or null for the default parseraliasManager- the alias manager, or null for no alias supportlineExpander- the line expander, or null for no variable expansionscriptRunner- the script runner, or null for no script supportparser- the argument parser, or null for the default parser
-
-
Method Detail
-
addGroup
public void addGroup(CommandGroup group)
Description copied from interface:CommandDispatcherAdds a command group to this dispatcher.- Specified by:
addGroupin interfaceCommandDispatcher- Parameters:
group- the command group to add
-
groups
public java.util.List<CommandGroup> groups()
Description copied from interface:CommandDispatcherReturns all registered command groups.- Specified by:
groupsin interfaceCommandDispatcher- Returns:
- the list of command groups
-
execute
public java.lang.Object execute(java.lang.String line) throws java.lang.ExceptionDescription copied from interface:CommandDispatcherExecutes a command line string.The dispatcher parses the line (potentially into a pipeline) and executes the commands.
- Specified by:
executein interfaceCommandDispatcher- 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:CommandDispatcherExecutes a pre-parsed pipeline.- Specified by:
executein interfaceCommandDispatcher- 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:
completerin interfaceCommandDispatcher- 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:CommandDispatcherReturns a description for the command at the current command line position.Used by widgets for context-sensitive help.
- Specified by:
describein interfaceCommandDispatcher- Parameters:
commandLine- the parsed command line- Returns:
- the command description, or null
-
terminal
public Terminal terminal()
Description copied from interface:CommandDispatcherReturns the terminal associated with this dispatcher.- Specified by:
terminalin interfaceCommandDispatcher- Returns:
- the terminal
-
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.ExceptionDescription copied from interface:CommandDispatcherInitializes the dispatcher, optionally executing a startup script.- Specified by:
initializein interfaceCommandDispatcher- 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceCommandDispatcher
-
-