Class PosixCommandsRegistry
java.lang.Object
org.jline.builtins.PosixCommandsRegistry
Registry for POSIX commands that provides a convenient way to register and execute
POSIX commands in JLine applications.
This class acts as a bridge between command frameworks and the PosixCommands implementations, making it easy to integrate POSIX commands into any command-line application.
Usage Example:
Terminal terminal = TerminalBuilder.builder().build();
Path currentDir = Paths.get(".");
PosixCommandsRegistry registry = new PosixCommandsRegistry(
terminal.input(),
new PrintStream(terminal.output()),
new PrintStream(terminal.output()),
currentDir,
terminal
);
// Execute a command
registry.execute("ls", new String[]{"ls", "-l"});
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for command implementations that can throw exceptions. -
Constructor Summary
ConstructorsConstructorDescriptionPosixCommandsRegistry(InputStream in, PrintStream out, PrintStream err, Path currentDir, org.jline.terminal.Terminal terminal, Function<String, Object> variables) Create a new POSIX commands registry.Create a new POSIX commands registry with a pre-built context. -
Method Summary
Modifier and TypeMethodDescriptionvoidExecute a command with a command line string.voidExecute a command.String[]Get all registered command names.Get the execution context.booleanhasCommand(String name) Check if a command is registered.voidPrint help for all available commands.voidPrint help for a specific command.voidregister(String name, PosixCommandsRegistry.CommandFunction command) Register a command with the registry.voidRegister all default POSIX commands.voidunregister(String name) Unregister a command from the registry.withCurrentDirectory(Path newCurrentDir) Create a new registry with a different current directory.
-
Constructor Details
-
PosixCommandsRegistry
public PosixCommandsRegistry(InputStream in, PrintStream out, PrintStream err, Path currentDir, org.jline.terminal.Terminal terminal, Function<String, Object> variables) Create a new POSIX commands registry.- Parameters:
in- input stream for commandsout- output stream for commandserr- error stream for commandscurrentDir- current working directoryterminal- terminal instance (can be null for non-interactive use)
-
PosixCommandsRegistry
Create a new POSIX commands registry with a pre-built context.- Parameters:
context- the execution context
-
-
Method Details
-
registerDefaultCommands
public void registerDefaultCommands()Register all default POSIX commands. -
register
Register a command with the registry.- Parameters:
name- command namecommand- command implementation
-
unregister
Unregister a command from the registry.- Parameters:
name- command name to unregister
-
hasCommand
Check if a command is registered.- Parameters:
name- command name- Returns:
- true if the command is registered
-
getCommandNames
-
execute
Execute a command.- Parameters:
name- command nameargv- command arguments (including command name as argv[0])- Throws:
Exception- if command execution failsIllegalArgumentException- if command is not registered
-
execute
-
getContext
-
withCurrentDirectory
Create a new registry with a different current directory. This is useful for commands that need to operate in different directories.- Parameters:
newCurrentDir- the new current directory- Returns:
- a new registry with the updated directory
-
printHelp
public void printHelp()Print help for all available commands. -
printHelp
-