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

com.github.rinde.rinsim.cli.ArgumentParser Maven / Gradle / Ivy

There is a newer version: 4.4.6
Show newest version
/*
 * Copyright (C) 2011-2015 Rinde van Lon, iMinds-DistriNet, KU Leuven
 *
 * 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.github.rinde.rinsim.cli;

import java.util.List;

import com.github.rinde.rinsim.cli.CliException.CauseType;
import com.github.rinde.rinsim.cli.Option.OptionArg;
import com.google.common.base.Converter;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;

/**
 * An argument parser is responsible for converting strings into the expected
 * type. If the parsing fails, a {@link CliException} is thrown. This class
 * contains a number of {@link ArgumentParser}s for common types.
 * @param  The argument type.
 * @author Rinde van Lon 
 */
public abstract class ArgumentParser {
  /**
   * Separator of argument lists.
   */
  public static final char ARG_LIST_SEPARATOR = ',';

  /**
   * {@link Boolean} parser.
   */
  public static final ArgumentParser BOOLEAN = new ArgumentParser(
      "boolean") {
    @Override
    Boolean parse(OptionArg option, String arg) {
      if ("T".equalsIgnoreCase(arg) || "true".equalsIgnoreCase(arg)
          || "1".equals(arg)) {
        return true;
      } else if ("F".equalsIgnoreCase(arg) || "false".equalsIgnoreCase(arg)
          || "0".equals(arg)) {
        return false;
      }
      throw new CliException("Expected a boolean but found: '" + arg + "'.",
          CauseType.INVALID_ARG_FORMAT, option);
    }
  };

  /**
   * {@link Long} parser.
   */
  public static final ArgumentParser LONG = new NumberParser<>(
      "long", Longs.stringConverter());

  /**
   * List of {@link Long}s parser.
   */
  public static final ArgumentParser> LONG_LIST = new NumberListParser<>(
      "long list", Longs.stringConverter());

  /**
   * {@link Integer} parser.
   */
  public static final ArgumentParser INTEGER = new NumberParser<>(
      "int", Ints.stringConverter());

  /**
   * List of {@link Integer}s parser.
   */
  public static final ArgumentParser> INTEGER_LIST = new NumberListParser<>(
      "int list", Ints.stringConverter());

  /**
   * {@link Double} parser.
   */
  public static final ArgumentParser DOUBLE = new NumberParser<>(
      "double", Doubles.stringConverter());

  /**
   * List of {@link Double}s parser.
   */
  public static final ArgumentParser> DOUBLE_LIST = new NumberListParser<>(
      "double list", Doubles.stringConverter());

  /**
   * {@link String} parser.
   */
  public static final ArgumentParser STRING = new ArgumentParser(
      "string") {
    @Override
    String parse(OptionArg option, String value) {
      return value;
    }
  };

  /**
   * List of {@link String}s parser.
   */
  public static final ArgumentParser> STRING_LIST = new ArgumentParser>(
      "string list") {
    @Override
    List parse(OptionArg> option, String value) {
      return Splitter.on(ARG_LIST_SEPARATOR).splitToList(value);
    }
  };

  private final String name;

  ArgumentParser(String nm) {
    name = nm;
  }

  abstract V parse(OptionArg option, String arg);

  String name() {
    return name;
  }

  static CliException convertNFE(Option option, NumberFormatException e,
      String value, String argName) {
    return new CliException(String.format(
        "The option %s expects a %s, found '%s'.", option, argName,
        value), e, CauseType.INVALID_ARG_FORMAT, option);
  }

  /**
   * A parser for numbers.
   * @param  The type of number.
   * @author Rinde van Lon 
   */
  public static class NumberParser extends ArgumentParser {
    private final Converter converter;

    /**
     * Construct a new parser.
     * @param nm The name of the argument, shown in the help menu. name.
     * @param conv The {@link Converter} to use to convert strings to type
     *          T.
     */
    public NumberParser(String nm, Converter conv) {
      super(nm);
      converter = conv;
    }

    @SuppressWarnings("null")
    @Override
    T parse(OptionArg option, String arg) {
      try {
        return converter.convert(arg);
      } catch (final NumberFormatException e) {
        throw convertNFE(option, e, arg, name());
      }
    }
  }

  /**
   * A parser for lists of numbers.
   * @param  The type of number.
   * @author Rinde van Lon 
   */
  public static class NumberListParser extends ArgumentParser> {
    private final Converter converter;

    /**
     * Construct a new list parser.
     * @param nm The name of the argument, shown in the help menu.
     * @param conv The {@link Converter} to use to convert strings to type
     *          T.
     */
    public NumberListParser(String nm, Converter conv) {
      super(nm);
      converter = conv;
    }

    @Override
    List parse(OptionArg> option, String value) {
      final Iterable strings = Splitter.on(ARG_LIST_SEPARATOR).split(
          value);
      try {
        return ImmutableList.copyOf(Iterables.transform(strings, converter));
      } catch (final NumberFormatException e) {
        throw convertNFE(option, e, value, name());
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy