public void testNoArgumentsOption() throws Exception { ClassLoader cl = TestCommandLineParsing.class.getClassLoader(); PluginDefinition pluginDef = PluginRepositoryUtil.parseXmlPluginDefinition(cl, cl.getResourceAsStream("check_mysql_plugin.xml")); GroupBuilder gBuilder = new GroupBuilder(); for (PluginOption po : pluginDef.getOptions()) { gBuilder = gBuilder.withOption(po.toOption()); } Group group = gBuilder.create(); Parser p = new Parser(); p.setGroup(group); CommandLine cli = p.parse(new String[] { "--hostname", "$ARG1$", "--port", "$ARG2$", "--database", "$ARG3$", "--user", "$ARG4$", "--password", "$ARG5$", "--check-slave" }); Assert.assertTrue(cli.hasOption("--check-slave")); }
public String getCommandLine() { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Group g = getCommandLineGroup(); HelpFormatter hf = new HelpFormatter(null, null, null, getConsole().getTerminal().getWidth()); hf.setGroup(g); hf.setPrintWriter(new PrintWriter(new OutputStreamWriter(bout, charset))); hf.printUsage(); String usage = new String(bout.toByteArray(), charset); String[] lines = usage.split("\\n"); StringBuilder res = new StringBuilder(); for (int i = 1; i < lines.length; i++) { res.append(lines[i]); } return res.toString(); }
public void printHelp() throws IOException { Group g = getCommandLineGroup(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); HelpFormatter hf = new HelpFormatter(" ", null, null, getConsole().getTerminal().getWidth()); hf.setGroup(g); PrintWriter pw = new PrintWriter(new OutputStreamWriter(bout, charset)); hf.setPrintWriter(pw); hf.printHelp(); pw.flush(); // getConsole().println("\u001B[1mCommand Line:\u001B[0m "); getConsole().println(highlight("Command Line: ")); getConsole().println(" " + getName() + " " + getCommandLine()); getConsole().println(highlight("Usage:")); getConsole().println(new String(bout.toByteArray(), charset)); }
/** * Parses the command line. * * @param vsArgs * The command line * @return The parsed command line */ private static CommandLine parseCommandLine(final String[] vsArgs) { try { Group opts = configureCommandLine(); // configure a HelpFormatter HelpFormatter hf = new HelpFormatter(); // configure a parser Parser p = new Parser(); p.setGroup(opts); p.setHelpFormatter(hf); // p.setHelpTrigger("--help"); return p.parse(vsArgs); } catch (OptionException oe) { printUsage(oe); } catch (Exception e) { e.printStackTrace(); // Should never happen... } return 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); } }
/** * Print the options supported by {@code GenericOptionsParser}. * In addition to the options supported by the job, passed in as the * group parameter. * * @param group job-specific command-line options. */ public static void printHelpWithGenericOptions(Group group) throws IOException { new GenericOptionsParser(new Configuration(), new org.apache.commons.cli.Options(), new String[0]); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charsets.UTF_8), true); HelpFormatter formatter = new HelpFormatter(); formatter.setGroup(group); formatter.setPrintWriter(pw); formatter.setFooter("Specify HDFS directories while running on hadoop; else specify local file system directories"); formatter.print(); }
public static void printHelpWithGenericOptions(Group group, OptionException oe) throws IOException { new GenericOptionsParser(new Configuration(), new org.apache.commons.cli.Options(), new String[0]); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charsets.UTF_8), true); HelpFormatter formatter = new HelpFormatter(); formatter.setGroup(group); formatter.setPrintWriter(pw); formatter.setException(oe); formatter.print(); }
private Group getCommandLineGroup() { PluginProxy pp = (PluginProxy) plugin; GroupBuilder gBuilder = new GroupBuilder(); for (PluginOption po : pp.getOptions()) { gBuilder = gBuilder.withOption(toOption(po)); } return gBuilder.create(); }
/** * Configure the command line parser. * * @return The configuration */ private static Group configureCommandLine() { DefaultOptionBuilder oBuilder = new DefaultOptionBuilder(); ArgumentBuilder aBuilder = new ArgumentBuilder(); GroupBuilder gBuilder = new GroupBuilder(); DefaultOption listOption = oBuilder.withLongName("list").withShortName("l").withDescription("Lists all the installed plugins").create(); DefaultOption versionOption = oBuilder.withLongName("version").withShortName("v").withDescription("Print the server version number").create(); DefaultOption helpOption = oBuilder.withLongName("help").withShortName("h").withDescription("Show this help").create(); // DefaultOption pluginNameOption = oBuilder.withLongName("plugin") // .withShortName("p").withDescription("The plugin name") // .withArgument( // aBuilder.withName("name").withMinimum(1).withMaximum(1) // .create()).create(); DefaultOption pluginHelpOption = oBuilder.withLongName("help").withShortName("h").withDescription("Shows help about a plugin") .withArgument(aBuilder.withName("name").withMinimum(1).withMaximum(1).create()).create(); Group alternativeOptions = gBuilder.withOption(listOption).withOption(pluginHelpOption).create(); DefaultOption confOption = oBuilder.withLongName("conf").withShortName("c").withDescription("Specifies the JNRPE configuration file") .withArgument(aBuilder.withName("path").withMinimum(1).withMaximum(1).create()).withChildren(alternativeOptions).withRequired(true) .create(); DefaultOption interactiveOption = oBuilder.withLongName("interactive").withShortName("i") .withDescription("Starts JNRPE in command line mode").create(); Group jnrpeOptions = gBuilder.withOption(confOption).withOption(interactiveOption).withMinimum(1).create(); return gBuilder.withOption(versionOption).withOption(helpOption).withOption(jnrpeOptions).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 Group getGroup() { return group; }
public static void printHelp(Group group) { HelpFormatter formatter = new HelpFormatter(); formatter.setGroup(group); formatter.print(); }
@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; }
/** * Configures the command line parser. * * @return The command line parser configuration */ private static Group configureCommandLine() { DefaultOptionBuilder oBuilder = new DefaultOptionBuilder(); ArgumentBuilder aBuilder = new ArgumentBuilder(); GroupBuilder gBuilder = new GroupBuilder(); DefaultOption nosslOption = oBuilder.withLongName("nossl").withShortName("n").withDescription("Do no use SSL").create(); DefaultOption weakSslOption = oBuilder.withLongName("weakCiherSuites").withShortName("w").withDescription("Enable weak cipher suites").create(); DefaultOption unknownOption = oBuilder.withLongName("unknown").withShortName("u") .withDescription("Make socket timeouts return an UNKNOWN state instead of CRITICAL").create(); DefaultOption hostOption = oBuilder.withLongName("host").withShortName("H") .withDescription("The address of the host running the JNRPE/NRPE daemon") .withArgument(aBuilder.withName("host").withMinimum(1).withMaximum(1).create()).create(); NumberValidator positiveInt = NumberValidator.getIntegerInstance(); positiveInt.setMinimum(0); DefaultOption portOption = oBuilder .withLongName("port") .withShortName("p") .withDescription("The port on which the daemon is running (default=5666)") .withArgument( aBuilder.withName("port").withMinimum(1).withMaximum(1).withDefault(Long.valueOf(DEFAULT_PORT)).withValidator(positiveInt) .create()).create(); DefaultOption timeoutOption = oBuilder .withLongName("timeout") .withShortName("t") .withDescription("Number of seconds before connection times out (default=10)") .withArgument( aBuilder.withName("timeout").withMinimum(1).withMaximum(1).withDefault(Long.valueOf(DEFAULT_TIMEOUT)) .withValidator(positiveInt).create()).create(); DefaultOption commandOption = oBuilder .withLongName("command") .withShortName("c") .withDescription("The name of the command that the remote daemon should run") .withArgument( aBuilder .withName("command") .withMinimum(1) .withMaximum(1).create() ) .create(); DefaultOption argsOption = oBuilder .withLongName("arglist") .withShortName("a") .withDescription( "Optional arguments that should be passed to the command. Multiple arguments should be separated by " + "a space (' '). If provided, this must be the last option supplied on the command line.") .withArgument(aBuilder.withName("arglist").withMinimum(1).create()).create(); DefaultOption helpOption = oBuilder.withLongName("help").withShortName("h").withDescription("Shows this help").create(); Group executionOption = gBuilder .withOption(nosslOption) .withOption(weakSslOption) .withOption(unknownOption) .withOption(hostOption) .withOption(portOption) .withOption(timeoutOption) .withOption(commandOption) .withOption(argsOption).create(); return gBuilder.withOption(executionOption).withOption(helpOption).withMinimum(1).withMaximum(1).create(); }