com.jonasjberg.voltdivx.arguments.ArgumentParser.java Source code

Java tutorial

Introduction

Here is the source code for com.jonasjberg.voltdivx.arguments.ArgumentParser.java

Source

/*                          ----------------------
 *                                 voltdivx
 *                          ----------------------
 *                       Copyright(c)2015 Jonas Sjberg
 *                       https://github.com/jonasjberg
 *                            jomeganas@gmail.com
 *______________________________________________________________________________
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *                    (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *______________________________________________________________________________
 */

package com.jonasjberg.voltdivx.arguments;

import com.jonasjberg.voltdivx.Main;
import com.jonasjberg.voltdivx.constants.ValueSeries;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;

/**
 * Created by spock on 11/21/15.
 */
public class ArgumentParser {
    private String[] args;
    private ArgumentContents contents;

    public ArgumentParser(String[] args, ArgumentContents contents) {
        this.args = args;
        this.contents = contents;
    }

    public void parse() {
        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = null;

        SiUnitParser siParser = new SiUnitParser();

        try {
            cmd = parser.parse(ArgumentOptions.getOptions(), args);

            if (cmd.hasOption("h") || cmd.getOptions().length == 0) {
                Main.printLogo();
                Help.displayHelp();
                Main.exit(0);
            }

            else {
                if (cmd.hasOption("i")) {
                    double opt = siParser.parse(cmd.getOptionValue("i"));
                    contents.setInputVoltage(opt);
                }

                if (cmd.hasOption("o")) {
                    double opt = siParser.parse(cmd.getOptionValue("o"));
                    contents.setOutputVoltage(opt);
                }

                if (cmd.hasOption("g")) {
                    double opt = siParser.parse(cmd.getOptionValue("g"));
                    contents.setGroundVoltage(opt);
                }

                if (cmd.hasOption("r")) {
                    double opt = siParser.parse(cmd.getOptionValue("r"));
                    contents.setR1Resistance(opt);
                }

                if (cmd.hasOption("t")) {
                    double opt = siParser.parse(cmd.getOptionValue("t"));
                    contents.setR2Resistance(opt);
                }

                if (cmd.hasOption("s")) {
                    String opt = cmd.getOptionValue("s");
                    contents.setValueSeries(parseValueSeries(opt));

                    /* Set tolerancePercent to the tolerance of the selected value
                     * series as default if the error option is not used. */
                    //  if (!cmd.hasOption("e")) {
                    //      double tolerancePercent = 100 * contents.getValueSeries().getTolerance();
                    //      System.out.println("tolerancePercent = " + tolerancePercent);
                    //      contents.setMaxErrorPercent(tolerancePercent);
                    //  }

                } else {
                    contents.setValueSeries(null);
                }

                if (cmd.hasOption("l")) {
                    String opt = cmd.getOptionValue("l");
                    int i = Integer.parseInt(opt);

                    contents.setNumberOfLines(i);
                }

                if (cmd.hasOption("c")) {
                    double opt = siParser.parse(cmd.getOptionValue("c"));
                    contents.setMaxCurrent(opt);
                }

                if (cmd.hasOption("e")) {
                    String opt = cmd.getOptionValue("e");

                    if (opt.contains("%")) {
                        opt = opt.replaceAll("%", "").trim();
                        contents.setMaxErrorPercent(Double.parseDouble(opt));
                    } else {
                        contents.setMaxErrorVoltage(siParser.parse(opt));
                    }
                }

                if (cmd.hasOption("q")) {
                    contents.setFlagQuiet(true);
                }
            }

        } catch (ParseException e) {
            // e.printStackTrace();
            System.err.println("Caught exception:" + e.getMessage());
        }
    }

    private ValueSeries parseValueSeries(String str) {
        str = str.replaceAll("[^eE0-9]", "");
        str = str.toLowerCase();

        switch (str) {
        case ("e6"):
            return ValueSeries.E6Values;

        case ("e12"):
            return ValueSeries.E12Values;

        case ("e24"):
            return ValueSeries.E24Values;

        case ("e48"):
            return ValueSeries.E48Values;

        case ("e96"):
            return ValueSeries.E96Values;

        case ("e192"):
            return ValueSeries.E192Values;

        default:
            Main.log.msgWarning("Got erroneous ValueSeries parameter!");
            return null;
        }
    }
}