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

com.jetbrains.commandInterface.command.Option Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition python-community library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetbrains.commandInterface.command;

import com.google.common.base.Preconditions;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Command option.
 * It may have some long names (like --foo) and short (like -f), help text and arguments (if not flag option)
 *
 * @author Ilya.Kazakevich
 */
public final class Option {
  @NotNull
  private final List myLongNames = new ArrayList();
  @NotNull
  private final List myShortNames = new ArrayList();
  @Nullable
  private final Pair myArgumentAndQuantity;
  @NotNull
  private final Help myHelp;

  /**
   * @param argumentAndQuantity if option accepts argument, there should be pair of [argument_quantity, its_type_info]
   * @param help                option help
   * @param shortNames          option short names
   * @param longNames           option long names
   */
  public Option(@Nullable final Pair argumentAndQuantity,
                @NotNull final Help help,
                @NotNull final Collection shortNames,
                @NotNull final Collection longNames) {
    Preconditions
      .checkArgument(argumentAndQuantity == null || argumentAndQuantity.first > 0, "Illegal args and quantity: " + argumentAndQuantity);
    myArgumentAndQuantity = argumentAndQuantity;
    myShortNames.addAll(shortNames);
    myLongNames.addAll(longNames);
    myHelp = help;
  }

  /**
   * @return Option long names
   */
  @NotNull
  public List getLongNames() {
    return Collections.unmodifiableList(myLongNames);
  }

  /**
   * @return all option names (long and short)
   */
  @NotNull
  public List getAllNames() {
    final List result = new ArrayList(myLongNames);
    result.addAll(myShortNames);
    return result;
  }

  /**
   * @return Option short names
   */
  @NotNull
  public List getShortNames() {
    return Collections.unmodifiableList(myShortNames);
  }

  // TODO: USe "known arguments info" to prevent copy/paste
  /**
   * @return if option accepts argument -- pair of [argument_quantity, argument]. Null otherwise.
   * Unlike position argument, option argument is always mandatory
   */
  @Nullable
  public Pair getArgumentAndQuantity() {
    return myArgumentAndQuantity;
  }

  /**
   * @return Option help
   */
  @NotNull
  public Help getHelp() {
    return myHelp;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy