All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.senzing.cmdline.RepeatedOptionException Maven / Gradle / Ivy

The newest version!
package com.senzing.cmdline;

import java.util.Set;

/**
 * Thrown when a command-line option is illegally specified more than once.
 */
public class RepeatedOptionException extends CommandLineException {
  /**
   * The option that was repeated.
   */
  private CommandLineOption option;

  /**
   * The flags used to specify the option.
   */
  private Set flags;

  /**
   * Constructs with the {@link CommandLineOption} that was repeated and the
   * flags used to specify it.  This is constructed with a {@link Set} of
   * flags that were used in case the same option can be specified with synonym
   * flags.
   *
   * @param option The {@link CommandLineOption} that was not repeated.
   * @param flags The {@link Set} of flags that were used to specify the option.
   */
  public RepeatedOptionException(CommandLineOption option, Set flags)
  {
    super(buildErrorMessage(option, flags));
    this.option = option;
    this.flags  = (flags == null) ? Set.of() : Set.copyOf(flags);
  }

  /**
   * Return the {@link CommandLineOption} that was repeated.
   *
   * @return The {@link CommandLineOption} that was repeated.
   */
  public CommandLineOption getOption() {
    return this.option;
  }

  /**
   * Returns the unmodifiable {@link Set} of command-line flags used to
   * specify the repeated {@link CommandLineOption}.
   *
   * @return The unmodifiable {@link Set} of command-line flags used to
   *         specify the repeated {@link CommandLineOption}.
   */
  public Set getFlags() {
    return this.flags;
  }

  /**
   * Builds the error message describing the repeated option.
   *
   * @param option The {@link CommandLineOption}.
   *
   * @param flags The {@link Set} of {@link String} command-line flags that
   *              were specified.
   *
   * @return The formatted error message.
   *
   * @throws NullPointerException If either of the specified parameters is
   *                              null.
   *
   * @throws IllegalArgumentException If the specified {@link Set} is empty.
   */
  public static String buildErrorMessage(CommandLineOption  option,
                                         Set        flags)
      throws IllegalArgumentException
  {
    StringBuilder sb = new StringBuilder("Option specified more than once: ");

    String  prefix  = "";
    int     count   = 0;

    for (String repeatedFlag: flags) {
      sb.append(prefix).append(repeatedFlag);
      count++;
      prefix = (count == flags.size()) ? " and " : ", ";
    }

    return sb.toString();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy