Example usage for org.apache.commons.cli CommandLine getOptionValues

List of usage examples for org.apache.commons.cli CommandLine getOptionValues

Introduction

In this page you can find the example usage for org.apache.commons.cli CommandLine getOptionValues.

Prototype

public String[] getOptionValues(char opt) 

Source Link

Document

Retrieves the array of values, if any, of an option.

Usage

From source file:de.haber.xmind2latex.cli.CliParameters.java

/**
 * Helper method that throws a {@link ParseException} if parameter arguments are missing.
 * /*  w  w  w.j  a  v  a2 s .  c o  m*/
 * @param cmd the concrete {@link CommandLine}
 * @param param name of the parameter that is to be validated
 * @param opts configured {@link Options}
 * @throws ParseException if the number of arguments does not correspond to the expected number of arguments for the given parameter
 */
public static void validateNumberOfArguments(CommandLine cmd, char param, Options opts) throws ParseException {
    Integer numberOfArgs = param2arguments.get(param);
    String[] tmp = cmd.getOptionValues(param);
    if (numberOfArgs != null && tmp != null && tmp.length % numberOfArgs != 0) {
        Option o = opts.getOption(Character.toString(param));
        String name = o.getLongOpt() != null ? o.getLongOpt() : Character.toString(param);
        throw new ParseException("Invalid amount of arguments for parameter " + name + ": <" + o.getArgName()
                + ">. Description: " + o.getDescription());
    }
}

From source file:br.usp.poli.lta.cereda.spa2run.Utils.java

static List<Metric> fromFilesToMetrics(CommandLine line) throws Exception {
    List<Metric> result = new ArrayList<>();
    if (line.hasOption("i")) {
        for (String s : line.getOptionValues("i")) {
            File file = new File(s);
            if (!file.exists()) {
                throw new Exception("file has to exist!");
            }/*ww w.ja va 2  s  . c  o m*/
            Yaml yaml = new Yaml();
            result.add(yaml.loadAs(new FileReader(file), Metric.class));
        }
    }
    return result;
}

From source file:com.dattack.dbtools.drules.DrulesClient.java

private static void execute(final String[] args) throws ConfigurationException, DrulesNestableException {

    final Options options = createOptions();

    try {/* w  ww .  ja  va2 s  . co  m*/
        final CommandLineParser parser = new DefaultParser();
        final CommandLine cmd = parser.parse(options, args);
        final String filename = cmd.getOptionValue(DRULES_OPTION);

        CompositeConfiguration configuration = null;
        if (cmd.hasOption(PROPERTIES_OPTION)) {
            configuration = loadProperties(cmd.getOptionValues(PROPERTIES_OPTION));
        }

        final DrulesEngine engine = new DrulesEngine(filename, configuration);

        if (cmd.hasOption(TASK_OPTION)) {
            for (final String taskName : cmd.getOptionValues(TASK_OPTION)) {
                final Identifier taskId = new IdentifierBuilder().withValue(taskName).build();
                engine.execute(taskId);
            }
        } else {
            // list available tasks
            showTaskList(engine.listTasks());

        }
    } catch (@SuppressWarnings("unused") final ParseException e) {
        showUsage(options);
    }
}

From source file:com.mycompany.localetopdf.CmdParser.java

public static boolean ConfigureAppEnviroment(String[] args, AppEnviroment AppEnvInst)
        throws IOException, FOPException, TransformerException {
    {//from   w  w w.  java 2 s  .c  o  m
        CmdParser instance = new CmdParser();

        CommandLineParser parser = new DefaultParser();

        try {
            CommandLine cmdLine = parser.parse(instance.getOptions(), args, true);

            if (cmdLine.hasOption("help")) {
                instance.printHelp();
                return false;
            } else if (cmdLine.hasOption("l")) {
                String[] localeArg = cmdLine.getOptionValues("locale");
                AppEnvInst.setLocale(new LocaleObject(localeArg[0], localeArg[1]));

                File PDF;
                if (cmdLine.hasOption("p")) {
                    PDF = new File(cmdLine.getOptionValue("p"));
                } else {
                    PDF = new File("Locale.pdf");
                }
                AppEnvInst.setOutputFile(PDF);

                File XSLT;
                if (cmdLine.hasOption("x")) {
                    XSLT = new File(cmdLine.getOptionValue("x"));
                } else {
                    XSLT = null;
                }

                boolean rewrite;
                if (cmdLine.hasOption("r")) {
                    rewrite = true;
                } else {
                    rewrite = false;
                }

                AppEnvInst.setView(new PdfView(XSLT, rewrite));
                return true;
            } else {
                instance.printHelp();
                return false;
            }

        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }
}

From source file:com.ignorelist.kassandra.steam.scraper.TaggerCli.java

private static Configuration toConfiguration(Configuration configuration, CommandLine commandLine)
        throws IOException {
    if (commandLine.hasOption("f")) {
        Set<Path> sharedConfigPaths = new LinkedHashSet<>();
        String[] passedPaths = commandLine.getOptionValues("f");
        if (0 == passedPaths.length) {
            throw new IllegalArgumentException("no paths passed in -f");
        }/*ww w .j a  v a2s.c  o m*/
        for (String p : passedPaths) {
            sharedConfigPaths.add(Paths.get(p));
        }
        configuration.setSharedConfigPaths(sharedConfigPaths);

    } else if (null == configuration.getSharedConfigPaths() || configuration.getSharedConfigPaths().isEmpty()) {
        configuration.setSharedConfigPaths(new PathResolver().findSharedConfig());
    }

    if (commandLine.hasOption("R")) {
        Path replacementFile = Paths.get(commandLine.getOptionValue("R"));
        configuration.setReplacements(replacementFile);
    }

    if (commandLine.hasOption("i")) {
        Path whiteListFile = Paths.get(commandLine.getOptionValue("i"));
        configuration.setWhiteList(whiteListFile);
    }

    if (commandLine.hasOption("I")) {
        configuration.setRemoveNotWhiteListed(true);
    }

    if (commandLine.hasOption("c") || commandLine.hasOption("g") || commandLine.hasOption("u")) {
        Set<TagType> tagTypes = new HashSet<>();
        final boolean addCategories = !commandLine.hasOption("c");
        if (addCategories) {
            tagTypes.add(TagType.CATEGORY);
        }
        final boolean addGenres = !commandLine.hasOption("g");
        if (addGenres) {
            tagTypes.add(TagType.GENRE);
        }
        final boolean addUserTags = commandLine.hasOption("u");
        if (addUserTags) {
            tagTypes.add(TagType.USER);
        }
        configuration.setTagTypes(tagTypes);

    } else if (null == configuration.getTagTypes() || configuration.getTagTypes().isEmpty()) {
        configuration.setTagTypes(Sets.newHashSet(TagType.CATEGORY, TagType.GENRE));
    }

    if (commandLine.hasOption("t")) {
        configuration.setDownloadThreads(Integer.valueOf(commandLine.getOptionValue("t")));
    } else if (null == configuration.getDownloadThreads()) {
        configuration.setDownloadThreads(Runtime.getRuntime().availableProcessors() + 1);
    }

    return configuration;
}

From source file:com.eucalyptus.upgrade.TestHarness.java

@SuppressWarnings("unchecked")
private static void runMethods(final CommandLine cmd, final Options opts) {
    String[] optVals = cmd.getOptionValues("test");
    for (int i = 0; i < optVals.length; i++) {
        String[] argParts = optVals[i].split(":");
        String className = argParts[0];
        String methodName = argParts[1];
        String[] methodArgsArray = new String[0];
        if (argParts.length > 2) {
            methodArgsArray = argParts[2].split(",");
            System.out.printf("Executing class:%s method:%s args:%s\n", className, methodName, argParts[2]);
        } else {/*from   ww w.ja  v a2s .c  o m*/
            System.out.printf("Executing class:%s method:%s\n", className, methodName);
        }
        Class[] params = new Class[methodArgsArray.length];
        for (int j = 0; j < params.length; j++) {
            params[j] = String.class;
        }

        try {
            Class clazz = Class.forName(className);
            clazz.getDeclaredMethod(methodName, params).invoke(null, (Object[]) methodArgsArray);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("Executed method");
    }
    // List<Class> testList = Lists.transform( Lists.newArrayList(
    // cmd.getOptionValues( "test" ) ), new Function<String, Class>( ) {
    // public Class apply( String arg ) {
    // String[] argParts = arg.split(":");
    // String className = argParts[0];
    // System.out.println("CLASS NAME:" + className);
    // Class targetClass = null;
    // try {
    // targetClass = Class.forName( className );
    // } catch ( Exception e ) {
    // try {
    // targetClass = Class.forName( className + "Test" );
    // } catch ( Exception e1 ) {
    // }
    // }
    // if( targetClass == null ) {
    // printHelp( opts );
    // System.exit( 1 );
    // } else {
    // for( int i = 1; i < argParts.length; i++ ) {
    // String property = argParts[i].replaceAll("=.*","");
    // String value = argParts[i].replaceAll(".*=","");
    // try {
    // targetClass.getDeclaredMethod( property, String.class ).invoke( null,
    // value );
    // } catch ( Exception e ) {
    // System.out.println( e );
    // System.exit( 1 );
    // }
    // }
    // }
    // return targetClass;
    // }
    // } );
    // return testList;
}

From source file:com.google.caja.plugin.Config.java

private static String[] getOptionValues(CommandLine cl, Option opt) {
    String[] values = cl.getOptionValues(opt.getOpt());
    return values != null ? values : new String[0];
}

From source file:com.asakusafw.compiler.bootstrap.OperatorCompilerDriver.java

private static void start(String[] args) throws Exception {
    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(OPTIONS, args);
    String sourcePath = cmd.getOptionValue(OPT_SOURCEPATH.getOpt());
    String output = cmd.getOptionValue(OPT_OUTPUT.getOpt());
    String encoding = cmd.getOptionValue(OPT_ENCODING.getOpt(), "UTF-8"); //$NON-NLS-1$
    String[] classes = cmd.getOptionValues(OPT_CLASSES.getOpt());

    List<Class<?>> operatorClasses = Lists.create();
    for (String className : classes) {
        Class<?> oc = Class.forName(className);
        operatorClasses.add(oc);//from  ww w  . ja  v a 2  s.  c  om
    }
    compile(new File(sourcePath), new File(output), Charset.forName(encoding), operatorClasses);
}

From source file:com.intuit.s3encrypt.S3Encrypt.java

private static void putS3Object(CommandLine cmd, AmazonS3EncryptionClient s3, String bucket, String filename,
        String keyname, String key) {
    String[] searchArgs = cmd.getOptionValues("put");
    System.out.println("Uploading a new object to S3 BUCKET = " + bucket + " FILENAME = " + filename);
    File file = new File(filename);
    PutObjectRequest request = new PutObjectRequest(bucket, filename, file);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.addUserMetadata(keyname, key);
    request.setMetadata(metadata);// w  ww .  ja va 2 s. com
    s3.putObject(request);
    System.out.println();
}

From source file:com.twosigma.beaker.core.Main.java

private static Map<String, List<String>> getPluginOptions(CommandLine options) {
    Map<String, List<String>> result = new HashMap<>();
    if (options.hasOption("plugin-option")) {
        for (String param : options.getOptionValues("plugin-option")) {
            int x = param.indexOf(':');
            if (x < 0) {
                throw new RuntimeException("plugin option requires colon (':')");
            }// w w  w. j a  v  a  2 s.c o m
            String key = param.substring(0, x);
            String val = param.substring(x + 1, param.length());
            List<String> current = result.get(key);
            if (null == current) {
                current = new ArrayList<>();
                result.put(key, current);
            }
            current.add(val);
        }
    }
    return result;
}