protected static Option buildOption(String name, String shortName, String description, boolean hasArg, int min, int max, boolean required, String defaultValue) { DefaultOptionBuilder optBuilder = new DefaultOptionBuilder().withLongName(name).withDescription(description) .withRequired(required); if (shortName != null) { optBuilder.withShortName(shortName); } if (hasArg) { ArgumentBuilder argBuilder = new ArgumentBuilder().withName(name).withMinimum(min).withMaximum(max); if (defaultValue != null) { argBuilder = argBuilder.withDefault(defaultValue); } optBuilder.withArgument(argBuilder.create()); } return optBuilder.create(); }
protected static void maybePut(Map<String, List<String>> args, CommandLine cmdLine, Option... opt) { for (Option o : opt) { // the option appeared on the command-line, or it has a value // (which is likely a default value). if (cmdLine.hasOption(o) || cmdLine.getValue(o) != null || (cmdLine.getValues(o) != null && !cmdLine.getValues(o).isEmpty())) { // nulls are ok, for cases where options are simple flags. List<?> vo = cmdLine.getValues(o); if (vo != null && !vo.isEmpty()) { List<String> vals = new ArrayList<String>(); for (Object o1 : vo) { vals.add(o1.toString()); } args.put(o.getPreferredName(), vals); } else { args.put(o.getPreferredName(), null); } } } }
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputOpt = DefaultOptionCreator.inputOption().withRequired(false).create(); Option outputOpt = DefaultOptionCreator.outputOption().withRequired(false).create(); Option vectorOpt = obuilder.withLongName("vector").withRequired(false).withArgument( abuilder.withName("v").withMinimum(1).withMaximum(1).create()).withDescription( "The vector implementation to use.").withShortName("v").create(); Option helpOpt = DefaultOptionCreator.helpOption(); Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(outputOpt).withOption( vectorOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } Path input = new Path(cmdLine.getValue(inputOpt, "testdata").toString()); Path output = new Path(cmdLine.getValue(outputOpt, "output").toString()); String vectorClassName = cmdLine.getValue(vectorOpt, "org.apache.mahout.math.RandomAccessSparseVector").toString(); //runJob(input, output, vectorClassName); } catch (OptionException e) { InputDriver.log.error("Exception parsing command line: ", e); CommandLineUtil.printHelp(group); } }
/** * @param name The name of the option * @return the {@link org.apache.commons.cli2.Option} with the name, else null */ protected Option getCLIOption(String name) { for (Option option : options) { if (option.getPreferredName().equals(name)) { return option; } } return null; }
/** * Convert this {@link PluginOption} to the Option required by Apache. * Commons Cli. * * @return The option object required by commons cli */ Option toOption() { DefaultOptionBuilder oBuilder = new DefaultOptionBuilder(); oBuilder.withShortName(option).withDescription(description).withRequired(required); if (longOptionName != null) { oBuilder.withLongName(longOptionName); } if (hasArgs) { ArgumentBuilder aBuilder = new ArgumentBuilder(); if (argName != null) { aBuilder.withName(argName); } if (argsAreOptional) { aBuilder.withMinimum(0); } if (argsCount != null) { aBuilder.withMaximum(argsCount); } else { aBuilder.withMaximum(1); } if (argsValueSeparator != null && argsValueSeparator.length() != 0) { aBuilder.withInitialSeparator(argsValueSeparator.charAt(0)); aBuilder.withSubsequentSeparator(argsValueSeparator.charAt(0)); } oBuilder.withArgument(aBuilder.create()); } return oBuilder.create(); }
private Option toOption(PluginOption po) { DefaultOptionBuilder oBuilder = new DefaultOptionBuilder(); oBuilder.withShortName(po.getOption()).withDescription(po.getDescription()).withRequired("true".equalsIgnoreCase(po.getRequired())); if (po.getLongOpt() != null) { oBuilder.withLongName(po.getLongOpt()); } if (po.hasArgs()) { ArgumentBuilder aBuilder = new ArgumentBuilder(); if (po.getArgName() != null) { aBuilder.withName(po.getArgName()); } if (po.getArgsOptional()) { aBuilder.withMinimum(0); } if (po.getArgsCount() != null) { aBuilder.withMaximum(po.getArgsCount()); } else { aBuilder.withMaximum(1); } if (po.getValueSeparator() != null && po.getValueSeparator().length() != 0) { aBuilder.withInitialSeparator(po.getValueSeparator().charAt(0)); aBuilder.withSubsequentSeparator(po.getValueSeparator().charAt(0)); } oBuilder.withArgument(aBuilder.create()); } return oBuilder.create(); }
@Override public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException { // TODO Auto-generated method stub DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputOpt = DefaultOptionCreator.inputOption().create(); Option datasetOpt = obuilder.withLongName("dataset").withShortName("ds").withRequired(true).withArgument( abuilder.withName("dataset").withMinimum(1).withMaximum(1).create()).withDescription("Dataset path") .create(); Option modelOpt = obuilder.withLongName("model").withShortName("m").withRequired(true).withArgument( abuilder.withName("path").withMinimum(1).withMaximum(1).create()). withDescription("Path to the Model").create(); Option outputOpt = DefaultOptionCreator.outputOption().create(); Option helpOpt = DefaultOptionCreator.helpOption(); Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(datasetOpt).withOption(modelOpt) .withOption(outputOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption("help")) { CommandLineUtil.printHelp(group); return -1; } dataName = cmdLine.getValue(inputOpt).toString(); String datasetName = cmdLine.getValue(datasetOpt).toString(); String modelName = cmdLine.getValue(modelOpt).toString(); String outputName = cmdLine.hasOption(outputOpt) ? cmdLine.getValue(outputOpt).toString() : null; if (log.isDebugEnabled()) { log.debug("inout : {}", dataName); log.debug("dataset : {}", datasetName); log.debug("model : {}", modelName); log.debug("output : {}", outputName); } dataPath = new Path(dataName); datasetPath = new Path(datasetName); modelPath = new Path(modelName); if (outputName != null) { outputPath = new Path(outputName); } } catch (OptionException e) { log.warn(e.toString(), e); CommandLineUtil.printHelp(group); return -1; } time = System.currentTimeMillis(); testModel(); time = System.currentTimeMillis() - time; writeToFileClassifyTime(Chi_RWCSUtils.elapsedTime(time)); return 0; }
public static void main(String[] args) throws IOException, DescriptorException { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option pathOpt = obuilder.withLongName("path").withShortName("p").withRequired(true).withArgument( abuilder.withName("path").withMinimum(1).withMaximum(1).create()).withDescription("Data path").create(); Option descriptorOpt = obuilder.withLongName("descriptor").withShortName("d").withRequired(true) .withArgument(abuilder.withName("descriptor").withMinimum(1).create()).withDescription( "data descriptor").create(); Option descPathOpt = obuilder.withLongName("file").withShortName("f").withRequired(true).withArgument( abuilder.withName("file").withMinimum(1).withMaximum(1).create()).withDescription( "Path to generated descriptor file").create(); Option regOpt = obuilder.withLongName("regression").withDescription("Regression Problem").withShortName("r") .create(); Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h") .create(); Group group = gbuilder.withName("Options").withOption(pathOpt).withOption(descPathOpt).withOption( descriptorOpt).withOption(regOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return; } String dataPath = cmdLine.getValue(pathOpt).toString(); String descPath = cmdLine.getValue(descPathOpt).toString(); List<String> descriptor = convert(cmdLine.getValues(descriptorOpt)); boolean regression = cmdLine.hasOption(regOpt); log.debug("Data path : {}", dataPath); log.debug("Descriptor path : {}", descPath); log.debug("Descriptor : {}", descriptor); log.debug("Regression : {}", regression); runTool(dataPath, descriptor, descPath, regression); } catch (OptionException e) { log.warn(e.toString()); CommandLineUtil.printHelp(group); } }
protected AbstractJob() { options = new LinkedList<Option>(); }
/** * Returns a default command line option for help. Used by all clustering jobs * and many others * */ public static Option helpOption() { return new DefaultOptionBuilder().withLongName("help") .withDescription("Print out help").withShortName("h").create(); }
@Override public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException { // TODO Auto-generated method stub DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option inputOpt = DefaultOptionCreator.inputOption().create(); Option datasetOpt = obuilder.withLongName("dataset").withShortName("ds").withRequired(true).withArgument( abuilder.withName("dataset").withMinimum(1).withMaximum(1).create()).withDescription("Dataset path") .create(); Option modelOpt = obuilder.withLongName("model").withShortName("m").withRequired(true).withArgument( abuilder.withName("path").withMinimum(1).withMaximum(1).create()). withDescription("Path to the Model").create(); Option outputOpt = DefaultOptionCreator.outputOption().create(); Option helpOpt = DefaultOptionCreator.helpOption(); Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(datasetOpt).withOption(modelOpt) .withOption(outputOpt).withOption(helpOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption("help")) { CommandLineUtil.printHelp(group); return -1; } dataName = cmdLine.getValue(inputOpt).toString(); String datasetName = cmdLine.getValue(datasetOpt).toString(); String modelName = cmdLine.getValue(modelOpt).toString(); String outputName = cmdLine.hasOption(outputOpt) ? cmdLine.getValue(outputOpt).toString() : null; if (log.isDebugEnabled()) { log.debug("inout : {}", dataName); log.debug("dataset : {}", datasetName); log.debug("model : {}", modelName); log.debug("output : {}", outputName); } dataPath = new Path(dataName); datasetPath = new Path(datasetName); modelPath = new Path(modelName); if (outputName != null) { outputPath = new Path(outputName); } } catch (OptionException e) { log.warn(e.toString(), e); CommandLineUtil.printHelp(group); return -1; } time = System.currentTimeMillis(); testModel(); time = System.currentTimeMillis() - time; writeToFileClassifyTime(Chi_RWUtils.elapsedTime(time)); return 0; }
/** Build an option with the given parameters. Name and description are * required. * * @param name the long name of the option prefixed with '--' on the command-line * @param shortName the short name of the option, prefixed with '-' on the command-line * @param description description of the option displayed in help method * @param hasArg true if the option has an argument. * @param required true if the option is required. * @param defaultValue default argument value, can be null. * @return the option. */ protected static Option buildOption(String name, String shortName, String description, boolean hasArg, boolean required, String defaultValue) { return buildOption(name, shortName, description, hasArg, 1, 1, required, defaultValue); }
/** Add an arbitrary option to the set of options this job will parse when * {@link #parseArguments(String[])} is called. If this option has no * argument, use {@code containsKey} on the map returned by * {@code parseArguments} to check for its presence. Otherwise, the * string value of the option will be placed in the map using a key * equal to this options long name preceded by '--'. * @return the option added. */ protected Option addOption(Option option) { options.add(option); return option; }