Class ShellBuilder
Shell instances.
The builder provides a fluent API for configuring the shell:
Shell shell = Shell.builder()
.terminal(terminal) // optional, auto-created if null
.prompt("myapp> ") // or .prompt(() -> dynamicPrompt)
.dispatcher(dispatcher) // optional, DefaultCommandDispatcher if null
.groups(group1, group2) // added to dispatcher
.parser(parser) // optional, DefaultParser by default
.historyFile(path) // optional
.variable(name, value) // forwarded to LineReader
.option(Option.X, true) // forwarded to LineReader
.onReaderReady((reader, dispatcher) -> { ... })// optional callback
.build();
- Since:
- 4.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaliasManager(AliasManager aliasManager) Sets the alias manager for the shell.build()Builds theShellinstance.commandHighlighter(boolean enable) Enables or disables the built-in command-aware syntax highlighter.dispatcher(CommandDispatcher dispatcher) Sets the command dispatcher.groups(CommandGroup... groups) Adds command groups to the dispatcher.helpCommands(boolean enable) Enables or disables built-in help commands (help).highlighter(Highlighter highlighter) Sets a custom highlighter for the shell'sLineReader.historyCommands(boolean enable) Enables or disables built-in history commands (history).historyFile(Path historyFile) Sets the history file path.initScript(File initScript) Sets the initialization script to execute on startup.jobManager(JobManager jobManager) Sets the job manager for the shell.lineExpander(LineExpander lineExpander) Sets the line expander for variable expansion.onReaderReady(BiConsumer<LineReader, CommandDispatcher> onReaderReady) Sets a callback invoked after the LineReader is created but beforeShell.run().onReaderReady(Consumer<LineReader> onReaderReady) Sets a callback invoked after the LineReader is created but beforeShell.run().option(LineReader.Option option, boolean value) Sets a LineReader option.optionCommands(boolean enable) Enables or disables built-in option commands (setopt,unsetopt,setvar).Sets the line parser.pipelineParser(PipelineParser pipelineParser) Sets a custom pipeline parser for the shell.Sets a static prompt string.Sets a dynamic prompt supplier, called before each line read.rightPrompt(String rightPrompt) Sets a static right prompt string.rightPrompt(Supplier<String> rightPromptSupplier) Sets a dynamic right prompt supplier, called before each line read.scriptCommands(boolean enable) Enables or disables built-in script commands (source,.).scriptRunner(ScriptRunner scriptRunner) Sets the script runner for executing script files.Sets the terminal for the shell.Sets a LineReader variable.variableCommands(boolean enable) Enables or disables built-in variable commands (set,unset,export).
-
Method Details
-
terminal
Sets the terminal for the shell. If not specified, a system terminal is created automatically.- Parameters:
terminal- the terminal- Returns:
- this builder
-
prompt
Sets a static prompt string.- Parameters:
prompt- the prompt string- Returns:
- this builder
-
prompt
Sets a dynamic prompt supplier, called before each line read.- Parameters:
promptSupplier- the prompt supplier- Returns:
- this builder
-
rightPrompt
Sets a static right prompt string.- Parameters:
rightPrompt- the right prompt string- Returns:
- this builder
-
rightPrompt
Sets a dynamic right prompt supplier, called before each line read.- Parameters:
rightPromptSupplier- the right prompt supplier- Returns:
- this builder
-
dispatcher
Sets the command dispatcher. If not specified, aDefaultCommandDispatcheris created.- Parameters:
dispatcher- the command dispatcher- Returns:
- this builder
-
groups
Adds command groups to the dispatcher.- Parameters:
groups- the command groups- Returns:
- this builder
-
parser
Sets the line parser. If not specified, aDefaultParseris used.- Parameters:
parser- the parser- Returns:
- this builder
-
historyFile
Sets the history file path.- Parameters:
historyFile- the path to the history file- Returns:
- this builder
-
variable
Sets a LineReader variable.- Parameters:
name- the variable namevalue- the variable value- Returns:
- this builder
-
option
Sets a LineReader option.- Parameters:
option- the optionvalue- the option value- Returns:
- this builder
-
onReaderReady
Sets a callback invoked after the LineReader is created but beforeShell.run().This is useful for setting up post-reader customizations.
- Parameters:
onReaderReady- the callback receiving the LineReader- Returns:
- this builder
- See Also:
-
onReaderReady
Sets a callback invoked after the LineReader is created but beforeShell.run().This is useful for setting up
CommandTailTipWidgetsor other post-reader customizations that need access to both the reader and the dispatcher.Shell.builder() .onReaderReady((reader, dispatcher) -> { new CommandTailTipWidgets(reader, dispatcher, 5).enable(); }) .build();- Parameters:
onReaderReady- the callback receiving the LineReader and CommandDispatcher- Returns:
- this builder
-
initScript
Sets the initialization script to execute on startup.- Parameters:
initScript- the init script file- Returns:
- this builder
-
jobManager
Sets the job manager for the shell.- Parameters:
jobManager- the job manager- Returns:
- this builder
-
pipelineParser
Sets a custom pipeline parser for the shell.This allows custom operator registration and subclass overrides of operator matching.
- Parameters:
pipelineParser- the pipeline parser- Returns:
- this builder
-
aliasManager
Sets the alias manager for the shell.When set, aliases are expanded before pipeline parsing, and built-in
alias/unaliascommands are registered automatically.- Parameters:
aliasManager- the alias manager- Returns:
- this builder
-
historyCommands
Enables or disables built-in history commands (history).- Parameters:
enable- true to enable- Returns:
- this builder
-
helpCommands
Enables or disables built-in help commands (help).- Parameters:
enable- true to enable- Returns:
- this builder
-
optionCommands
Enables or disables built-in option commands (setopt,unsetopt,setvar).- Parameters:
enable- true to enable- Returns:
- this builder
-
highlighter
Sets a custom highlighter for the shell'sLineReader.- Parameters:
highlighter- the highlighter- Returns:
- this builder
-
commandHighlighter
Enables or disables the built-in command-aware syntax highlighter.When enabled, known commands are highlighted in bold and unknown commands are highlighted in red.
- Parameters:
enable- true to enable- Returns:
- this builder
-
lineExpander
Sets the line expander for variable expansion.The line expander is called after alias expansion and before pipeline parsing.
- Parameters:
lineExpander- the line expander- Returns:
- this builder
-
scriptRunner
Sets the script runner for executing script files.- Parameters:
scriptRunner- the script runner- Returns:
- this builder
-
scriptCommands
Enables or disables built-in script commands (source,.).- Parameters:
enable- true to enable- Returns:
- this builder
-
variableCommands
Enables or disables built-in variable commands (set,unset,export).- Parameters:
enable- true to enable- Returns:
- this builder
-
build
Builds theShellinstance.If no terminal is provided, a system terminal is created. If no dispatcher is provided, a
DefaultCommandDispatcheris created. All configured groups are added to the dispatcher.- Returns:
- a new Shell
- Throws:
IOException- if terminal creation fails
-