Package org.jline.builtins
Class PosixCommandsRegistry
- java.lang.Object
-
- org.jline.builtins.PosixCommandsRegistry
-
public class PosixCommandsRegistry extends java.lang.ObjectRegistry 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 Classes Modifier and Type Class Description static interfacePosixCommandsRegistry.CommandFunctionFunctional interface for command implementations that can throw exceptions.
-
Constructor Summary
Constructors Constructor Description PosixCommandsRegistry(java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err, java.nio.file.Path currentDir, Terminal terminal, java.util.function.Function<java.lang.String,java.lang.Object> variables)Create a new POSIX commands registry.PosixCommandsRegistry(PosixCommands.Context context)Create a new POSIX commands registry with a pre-built context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecute(java.lang.String commandLine)Execute a command with a command line string.voidexecute(java.lang.String name, java.lang.String[] argv)Execute a command.java.lang.String[]getCommandNames()Get all registered command names.PosixCommands.ContextgetContext()Get the execution context.booleanhasCommand(java.lang.String name)Check if a command is registered.voidprintHelp()Print help for all available commands.voidprintHelp(java.lang.String commandName)Print help for a specific command.voidregister(java.lang.String name, PosixCommandsRegistry.CommandFunction command)Register a command with the registry.voidregisterDefaultCommands()Register all default POSIX commands.voidunregister(java.lang.String name)Unregister a command from the registry.PosixCommandsRegistrywithCurrentDirectory(java.nio.file.Path newCurrentDir)Create a new registry with a different current directory.
-
-
-
Constructor Detail
-
PosixCommandsRegistry
public PosixCommandsRegistry(java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err, java.nio.file.Path currentDir, Terminal terminal, java.util.function.Function<java.lang.String,java.lang.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
public PosixCommandsRegistry(PosixCommands.Context context)
Create a new POSIX commands registry with a pre-built context.- Parameters:
context- the execution context
-
-
Method Detail
-
registerDefaultCommands
public void registerDefaultCommands()
Register all default POSIX commands.
-
register
public void register(java.lang.String name, PosixCommandsRegistry.CommandFunction command)Register a command with the registry.- Parameters:
name- command namecommand- command implementation
-
unregister
public void unregister(java.lang.String name)
Unregister a command from the registry.- Parameters:
name- command name to unregister
-
hasCommand
public boolean hasCommand(java.lang.String name)
Check if a command is registered.- Parameters:
name- command name- Returns:
- true if the command is registered
-
getCommandNames
public java.lang.String[] getCommandNames()
Get all registered command names.- Returns:
- array of command names
-
execute
public void execute(java.lang.String name, java.lang.String[] argv) throws java.lang.ExceptionExecute a command.- Parameters:
name- command nameargv- command arguments (including command name as argv[0])- Throws:
java.lang.Exception- if command execution failsjava.lang.IllegalArgumentException- if command is not registered
-
execute
public void execute(java.lang.String commandLine) throws java.lang.ExceptionExecute a command with a command line string. This is a convenience method that splits the command line and calls execute.- Parameters:
commandLine- command line string (e.g., "ls -l /tmp")- Throws:
java.lang.Exception- if command execution fails
-
getContext
public PosixCommands.Context getContext()
Get the execution context.- Returns:
- the execution context
-
withCurrentDirectory
public PosixCommandsRegistry withCurrentDirectory(java.nio.file.Path newCurrentDir)
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
public void printHelp(java.lang.String commandName) throws java.lang.ExceptionPrint help for a specific command.- Parameters:
commandName- the command to get help for- Throws:
java.lang.Exception- if getting help fails
-
-