Package org.strassburger.tui4j.command
Class Command
java.lang.Object
org.strassburger.tui4j.command.Command
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddArgument(Argument<?>... argument) Adds one or more arguments to this command.Adds one or more options to this command.addSubCommand(Command... command) Adds one or more sub-commands to this command.
Example:voidParses the given arguments and executes the command's handler.getName()static CommandCreates a named command with the given name.static Commandroot()Creates a root command with no name.setHandler(CommandHandler handler) Sets the handler for this command.
-
Constructor Details
-
Command
-
-
Method Details
-
getName
-
getSubCommands
-
addSubCommand
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
-
addOption
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
-
addArgument
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
-
setHandler
Sets the handler for this command.- Parameters:
handler- The CommandHandler to set- Returns:
- The current Command instance for chaining
-
named
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
Creates a root command with no name. This command can have sub-commands, options, and arguments.- Returns:
- A new root Command instance
-
execute
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
-