Example #1
0
File: Cmd.java Project: Rojoss/Boxx
  /**
   * Register a regular command argument/parameter for this command.
   *
   * <p>When adding multiple arguments the order you add them in determines the indexing of the
   * argument!
   *
   * @param name The argument name/key used to identify the argument. This name must be used with
   *     the {@link CmdData} result to get the argument value.
   * @param requirement The requirement for this argument. (See {@link ArgRequirement} for more
   *     info)
   * @param option The {@link SingleOption} used for parsing the argument. This option determines
   *     the argument value and everything else. For example if it's a {@link PlayerO} the argument
   *     value must be a player and the result value would be a player.
   * @return The added {@link Argument}
   * @throws IllegalArgumentException if an argument with the specified name is already registered
   *     for this command if the argument option is a sub command option and the command already has
   *     an sub command argument or if the argument option is a sub command option and the command
   *     is a sub command.
   */
  public Argument addArgument(String name, ArgRequirement requirement, SingleOption option) {
    Argument argument = new Argument(name, requirement, option);

    if (getAllArguments().containsKey(name.toLowerCase())) {
      throw new IllegalArgumentException(
          "The command already has an argument with the name '" + name + "'!");
    }

    if (argument.option() instanceof SubCmdO) {
      if (isSub()) {
        throw new IllegalArgumentException(
            "Sub commands can not have sub command arguments. [argument=" + name + "]");
      }
      for (Argument arg : arguments.values()) {
        if (arg.option() instanceof SubCmdO) {
          throw new IllegalArgumentException(
              "The command already has a sub command argument."
                  + "Commands can only have one sub command option. [argument="
                  + name
                  + "]");
        }
      }
    }

    arguments.put(name.toLowerCase(), argument);
    return argument;
  }
Example #2
0
File: Cmd.java Project: Rojoss/Boxx
 /**
  * Try to get an array with sub commands that this command has.
  *
  * <p>If this command doesn't have sub commands this will return {@code null}. See {@link
  * #hasSubCmds()}
  *
  * @return Array with {@link SubCmd}s (May be {@code null} when the command doesn't have sub
  *     commands)
  */
 public SubCmd[] getSubCmds() {
   if (isSub() || arguments.size() < 1) {
     return null;
   }
   for (Argument arg : arguments.values()) {
     if (arg.option() instanceof SubCmdO) {
       return ((SubCmdO) arg.option()).getSubCmds();
     }
   }
   return null;
 }