001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.cli.processor.command;
007
008import org.apache.commons.cli.CommandLine;
009
010import dev.metaschema.cli.processor.CallingContext;
011import dev.metaschema.core.util.ObjectUtils;
012import edu.umd.cs.findbugs.annotations.NonNull;
013
014/**
015 * A base class for implementation that perform the operation supported by a
016 * command.
017 */
018public abstract class AbstractCommandExecutor implements ICommandExecutor {
019  @NonNull
020  private final CallingContext callingContext;
021  @NonNull
022  private final CommandLine commandLine;
023
024  /**
025   * Construct a new command executor.
026   *
027   * @param callingContext
028   *          the context of the command execution
029   * @param commandLine
030   *          the parsed command line details
031   */
032  protected AbstractCommandExecutor(
033      @NonNull CallingContext callingContext,
034      @NonNull CommandLine commandLine) {
035    this.callingContext = callingContext;
036    this.commandLine = commandLine;
037  }
038
039  /**
040   * Get the context of the command execution, which provides access to the
041   * execution environment needed for command processing.
042   *
043   * @return the context
044   */
045  @NonNull
046  protected CallingContext getCallingContext() {
047    return callingContext;
048  }
049
050  /**
051   * Get the parsed command line details containing the command options and
052   * arguments provided by the user during execution.
053   *
054   * @return the cli details
055   */
056  @NonNull
057  protected CommandLine getCommandLine() {
058    return commandLine;
059  }
060
061  @Override
062  public abstract void execute() throws CommandExecutionException;
063
064  /**
065   * Get the command associated with this execution.
066   *
067   * @return the command
068   */
069  @NonNull
070  protected ICommand getCommand() {
071    return ObjectUtils.requireNonNull(getCallingContext().getTargetCommand());
072  }
073}