Class CallingContext

java.lang.Object
dev.metaschema.cli.processor.CallingContext

public class CallingContext extends Object
Records information about the command line options and called command hierarchy.
  • Method Details

    • getCLIProcessor

      @NonNull public CLIProcessor getCLIProcessor()
      Get the command line processor instance that generated this calling context.
      Returns:
      the instance
    • getTargetCommand

      @Nullable public ICommand getTargetCommand()
      Get the command that was triggered by the CLI arguments.
      Returns:
      the command or null if no command was triggered
    • checkHelpAndVersion

      @NonNull protected Optional<ExitStatus> checkHelpAndVersion()
      Check for --help and --version options before full parsing.

      This is phase 1 of command processing.

      Returns:
      an exit status if help or version was requested, or empty to continue
    • parseOptions

      @NonNull protected org.apache.commons.cli.CommandLine parseOptions() throws org.apache.commons.cli.ParseException
      Parse all command line options.

      This is phase 2 of command processing.

      Returns:
      the parsed command line
      Throws:
      org.apache.commons.cli.ParseException - if parsing fails
    • validateExtraArguments

      @NonNull protected Optional<ExitStatus> validateExtraArguments(@NonNull org.apache.commons.cli.CommandLine cmdLine)
      Validate extra arguments for the target command.

      This is phase 3 of command processing.

      Parameters:
      cmdLine - the parsed command line
      Returns:
      an exit status if validation failed, or empty to continue
    • validateCalledCommands

      @NonNull protected Optional<ExitStatus> validateCalledCommands(@NonNull org.apache.commons.cli.CommandLine cmdLine)
      Validate options for all called commands in the chain.

      This is phase 4 of command processing.

      Parameters:
      cmdLine - the parsed command line
      Returns:
      an exit status if validation failed, or empty to continue
    • applyGlobalOptions

      protected void applyGlobalOptions(@NonNull org.apache.commons.cli.CommandLine cmdLine)
      Apply global options like --no-color and --quiet.

      This is phase 5 of command processing.

      Parameters:
      cmdLine - the parsed command line
    • processCommand

      @NonNull public ExitStatus processCommand()
      Process the command identified by the CLI arguments.
      Returns:
      the result of processing the command
    • handleError

      @NonNull public ExitStatus handleError(@NonNull ExitStatus exitStatus, @NonNull org.apache.commons.cli.CommandLine cmdLine, boolean showHelp)
      Handle an error that occurred while executing the command.
      Parameters:
      exitStatus - the execution result
      cmdLine - the command line information
      showHelp - if true show the help information
      Returns:
      the resulting exit status
    • handleInvalidCommand

      @NonNull public ExitStatus handleInvalidCommand(@NonNull String message)
      Generate the help message and exit status for an invalid command using the provided message.
      Parameters:
      message - the error message
      Returns:
      the resulting exit status
    • showHelp

      public void showHelp()
      Output the help text to the console.
      Throws:
      UncheckedIOException - if an error occurs while writing help output