Class Command

java.lang.Object
org.strassburger.tui4j.command.Command

public class Command extends Object
Represents a command in a command-line interface (CLI) application. A command can have sub-commands, options, arguments, and a handler to execute when the command is invoked.
  • Constructor Details

  • Method Details

    • getName

      public String getName()
    • getSubCommands

      public List<Command> getSubCommands()
    • addSubCommand

      public Command addSubCommand(Command... command)
      Adds one or more sub-commands to this command.
      Example:
      git commit
      can be represented as:
           Command git = Command.named("git")
               .addSubCommand(Command.named("commit"));
       
      Parameters:
      command - The Command(s) to add as sub-commands
      Returns:
      The current Command instance for chaining
    • getOptions

      public List<Option<?>> getOptions()
    • addOption

      public Command addOption(Option<?>... option)
      Adds one or more options to this command. Options are named parameters that the command can accept.
      Example:
      remove --force --file myfile
      can be represented as:
           Command cmd = Command.named("remove")
               .addOption(Option.bool("force", "force"), Option.str("file", "f", null));
       
      Parameters:
      option - The Option(s) to add
      Returns:
      The current Command instance for chaining
    • getArguments

      public List<Argument<?>> getArguments()
    • addArgument

      public Command addArgument(Argument<?>... argument)
      Adds one or more arguments to this command. Arguments are positional parameters that the command can accept.
      Example:
      copy source destination
      can be represented as:
           Command cmd = Command.named("copy")
               .addArgument(Argument.str("source", true), Argument.str("destination", true));
       
      Parameters:
      argument - The Argument(s) to add
      Returns:
      The current Command instance for chaining
    • getHandler

      public CommandHandler getHandler()
    • setHandler

      public Command setHandler(CommandHandler handler)
      Sets the handler for this command.
      Parameters:
      handler - The CommandHandler to set
      Returns:
      The current Command instance for chaining
    • named

      public static Command named(String name)
      Creates a named command with the given name. This command can have sub-commands, options, and arguments.
      Parameters:
      name - The name of the command
      Returns:
      A new Command instance with the specified name
    • root

      public static Command root()
      Creates a root command with no name. This command can have sub-commands, options, and arguments.
      Returns:
      A new root Command instance
    • execute

      public void execute(String[] args) throws CommandException
      Parses the given arguments and executes the command's handler. If a sub-command is detected, it delegates parsing and execution to the sub-command.
      Parameters:
      args - The command-line arguments to parse
      Throws:
      CommandException - if an error occurs during parsing or execution