Class PosixCommandsRegistry

java.lang.Object
org.jline.builtins.PosixCommandsRegistry

public class PosixCommandsRegistry extends Object
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"});
  • 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 commands
      out - output stream for commands
      err - error stream for commands
      currentDir - current working directory
      terminal - 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 Details

    • registerDefaultCommands

      public void registerDefaultCommands()
      Register all default POSIX commands.
    • register

      public void register(String name, PosixCommandsRegistry.CommandFunction command)
      Register a command with the registry.
      Parameters:
      name - command name
      command - command implementation
    • unregister

      public void unregister(String name)
      Unregister a command from the registry.
      Parameters:
      name - command name to unregister
    • hasCommand

      public boolean hasCommand(String name)
      Check if a command is registered.
      Parameters:
      name - command name
      Returns:
      true if the command is registered
    • getCommandNames

      public String[] getCommandNames()
      Get all registered command names.
      Returns:
      array of command names
    • execute

      public void execute(String name, String[] argv) throws Exception
      Execute a command.
      Parameters:
      name - command name
      argv - command arguments (including command name as argv[0])
      Throws:
      Exception - if command execution fails
      IllegalArgumentException - if command is not registered
    • execute

      public void execute(String commandLine) throws Exception
      Execute 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:
      Exception - if command execution fails
    • getContext

      public PosixCommands.Context getContext()
      Get the execution context.
      Returns:
      the execution context
    • withCurrentDirectory

      public PosixCommandsRegistry withCurrentDirectory(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(String commandName) throws Exception
      Print help for a specific command.
      Parameters:
      commandName - the command to get help for
      Throws:
      Exception - if getting help fails