hrytsenko.gscripts.AppArgs.java Source code

Java tutorial

Introduction

Here is the source code for hrytsenko.gscripts.AppArgs.java

Source

/*
 * #%L
 * gscripts
 * %%
 * Copyright (C) 2015 Anton Hrytsenko
 * %%
 * 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.
 * #L%
 */
package hrytsenko.gscripts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * Command-line arguments.
 * 
 * @author hrytsenko.anton
 */
public class AppArgs {

    private static final String APP_NAME = "gscripts";

    private static final String SCRIPTS_OPT_NAME = "s";
    private static final String SCRIPTS_ARGS_OPT_NAME = "a";

    private final List<String> scripts;
    private final List<String> scriptsArgs;

    /**
     * Create arguments.
     * 
     * @param scripts
     *            the list of scripts.
     * @param scriptsArgs
     *            the list of arguments for scripts.
     */
    private AppArgs(String[] scripts, String[] scriptsArgs) {
        this.scripts = Arrays.asList(scripts);
        this.scriptsArgs = Arrays.asList(scriptsArgs);
    }

    /**
     * Parse the command-line arguments.
     * 
     * @param args
     *            the list of arguments.
     * 
     * @return the parsed arguments.
     * 
     * @throws ParseException
     *             if command-line arguments could not be parsed.
     */
    public static AppArgs parseArgs(String[] args) throws ParseException {
        CommandLineParser parser = new DefaultParser();
        CommandLine line = parser.parse(getOptions(), args);

        String[] scripts = getValues(line, SCRIPTS_OPT_NAME);
        String[] scriptsArgs = getValues(line, SCRIPTS_ARGS_OPT_NAME);

        return new AppArgs(scripts, scriptsArgs);
    }

    private static String[] getValues(CommandLine line, String option) {
        return line.hasOption(option) ? line.getOptionValues(option) : new String[0];
    }

    private static Options getOptions() {
        Options options = new Options();
        options.addOption(createOption(SCRIPTS_OPT_NAME, "scripts to execute", true));
        options.addOption(createOption(SCRIPTS_ARGS_OPT_NAME, "arguments for scripts", false));
        return options;
    }

    private static Option createOption(String name, String description, boolean required) {
        Option option = new Option(name, true, description);
        option.setArgs(Option.UNLIMITED_VALUES);
        option.setRequired(required);
        return option;
    }

    /**
     * Print help.
     */
    public static void printHelp() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(APP_NAME, getOptions());
    }

    /**
     * Get list of scripts.
     * 
     * @return the list of filenames.
     */
    public List<String> getScripts() {
        return new ArrayList<>(scripts);
    }

    /**
     * Get list of arguments for scripts.
     * 
     * @return the list of string values.
     */
    public List<String> getScriptsArgs() {
        return new ArrayList<>(scriptsArgs);
    }

}