Class PosixCommandsRegistry


  • public class PosixCommandsRegistry
    extends java.lang.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"});
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  PosixCommandsRegistry.CommandFunction
      Functional 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, org.jline.terminal.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.
    • Constructor Detail

      • PosixCommandsRegistry

        public PosixCommandsRegistry​(java.io.InputStream in,
                                     java.io.PrintStream out,
                                     java.io.PrintStream err,
                                     java.nio.file.Path currentDir,
                                     org.jline.terminal.Terminal terminal,
                                     java.util.function.Function<java.lang.String,​java.lang.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 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 name
        command - 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.Exception
        Execute a command.
        Parameters:
        name - command name
        argv - command arguments (including command name as argv[0])
        Throws:
        java.lang.Exception - if command execution fails
        java.lang.IllegalArgumentException - if command is not registered
      • execute

        public void execute​(java.lang.String commandLine)
                     throws java.lang.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:
        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.Exception
        Print help for a specific command.
        Parameters:
        commandName - the command to get help for
        Throws:
        java.lang.Exception - if getting help fails