private static Params parseCommand(String[] args) { try { Options options = new Options(); options.addOption(new Option("l", "language", true, "language")); options.addOption(new Option("t", "thread", true, "thread")); options.addOption(new Option("f", "file", true, "script file")); options.addOption(new Option("i", "input", true, "input file")); options.addOption(new Option("s", "sleep", true, "sleep time")); options.addOption(new Option("g", "logger", true, "sleep time")); CommandLineParser commandLineParser = new PosixParser(); CommandLine commandLine = commandLineParser.parse(options, args); return readOptions(commandLine); } catch (Exception e) { e.printStackTrace(); exit(); return null; } }
Options options = new Options(); options.addOption(input); options.addOption(output); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd; cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("utility-name", options); String inputFilePath = cmd.getOptionValue("input"); String outputFilePath = cmd.getOptionValue("output");
private boolean parseCommandLine(String[] args) throws ParseException { Options options = new Options(); .addOption("h", "help", false, "display help text"); CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption('h')) { new HelpFormatter().printHelp("flume-ng avro-client", "", options, "The --dirname option assumes that a spooling directory exists " + "where immutable log files are dropped.", true); if (commandLine.hasOption("filename") && commandLine.hasOption("dirname")) { throw new ParseException( "--filename and --dirname options cannot be used simultaneously"); if (!commandLine.hasOption("port") && !commandLine.hasOption("host") && !commandLine.hasOption("rpcProps")) { throw new ParseException("Either --rpcProps or both --host and --port " + "must be specified."); if (!commandLine.hasOption("port")) { throw new ParseException( "You must specify a port to connect to with --port");
public static CommandLine parseCmdLine(final String appName, String[] args, Options options, CommandLineParser parser) { HelpFormatter hf = new HelpFormatter(); hf.setWidth(110); CommandLine commandLine = null; try { commandLine = parser.parse(options, args); if (commandLine.hasOption('h')) { hf.printHelp(appName, options, true); return null; } } catch (ParseException e) { hf.printHelp(appName, options, true); } return commandLine; }
public static void setFromCommandLineArgs(Options options, String[] args) { CommandLineParser parser = new PosixParser(); try { CommandLine cl = parser.parse(options, args); THE_INSTANCE.windowLength = new Duration(Integer.parseInt( cl.getOptionValue(LogAnalyzerAppMain.WINDOW_LENGTH, "30")) * 1000); THE_INSTANCE.slideInterval = new Duration(Integer.parseInt( cl.getOptionValue(LogAnalyzerAppMain.SLIDE_INTERVAL, "5")) * 1000); THE_INSTANCE.logsDirectory = cl.getOptionValue( LogAnalyzerAppMain.LOGS_DIRECTORY, "/tmp/logs"); THE_INSTANCE.outputHtmlFile = cl.getOptionValue( LogAnalyzerAppMain.OUTPUT_HTML_FILE, "/tmp/log_stats.html"); THE_INSTANCE.checkpointDirectory = cl.getOptionValue( LogAnalyzerAppMain.CHECKPOINT_DIRECTORY, "/tmp/log-analyzer-streaming"); THE_INSTANCE.indexHtmlTemplate = cl.getOptionValue( LogAnalyzerAppMain.INDEX_HTML_TEMPLATE, "./src/main/resources/index.html.template"); THE_INSTANCE.outputDirectory = cl.getOptionValue( LogAnalyzerAppMain.OUTPUT_DIRECTORY, "/tmp/pandaout"); THE_INSTANCE.initialized = true; } catch (ParseException e) { THE_INSTANCE.initialized = false; System.err.println("Parsing failed. Reason: " + e.getMessage()); } } }
Map<String, SlowExecutorPattern> topoSpecificImbalance = new HashMap<>(); try { cmd = parser.parse(options, args); if (cmd.hasOption("t")) { executeTime = Double.valueOf(cmd.getOptionValue("t")); if (cmd.hasOption("parallel")) { for (String stringParallel : cmd.getOptionValues("parallel")) { Matcher m = MULTI_PATTERN.matcher(stringParallel); if (!m.matches()) { throw new ParseException("--parallel " + stringParallel + " is not in the format MULTIPLIER(:TOPO:COMP)?"); Matcher m = MULTI_PATTERN.matcher(stringThroughput); if (!m.matches()) { throw new ParseException("--throughput " + stringThroughput + " is not in the format MULTIPLIER(:TOPO:COMP)?"); throw new ParseException(stringImbalance + " does not appear to match the expected pattern"); } else if (parts.length == 3) { topoSpecificImbalance.put(parts[1] + ":" + parts[2], SlowExecutorPattern.fromString(parts[0])); System.err.println("ERROR " + commandLineException.getMessage()); new HelpFormatter().printHelp("GenLoad [options] [captured_file]*", options); return;
Options opts = new Options(); .create("protocol"); opts.addOption(redopt); opts.addOption(outscopt); opts.addOption(popt); opts.addOption(pargs); opts.addOption(protoopt); CommandLineParser parser = new GnuParser(); HelpFormatter formatter = new HelpFormatter(); line = parser.parse(opts, argarry); if (line.hasOption("help")) { formatter.printHelp("tether", opts ); return 0; genargs = line.getArgs(); FileInputFormat.addInputPaths(job, line.getOptionValue("in")); FileOutputFormat.setOutputPath(job,new Path (line.getOptionValue("out"))); formatter.printHelp("tether", opts ); return -1;
CommandLineParser parser = new GnuParser(); Options r = buildGeneralOptions(new Options()); CommandLine commandLine = parser.parse(r, str2, true); if (commandLine.hasOption("n")) { workerNum = Integer.valueOf(commandLine.getOptionValue("n")); if (commandLine.hasOption("p")) { component = commandLine.getOptionValue("p");
Option option_S = OptionBuilder.withArgName("opt2").hasArg().withDescription("The S option").create("S"); Option option_test = new Option("test", "The test option"); Options options = new Options(); CommandLineParser parser = new GnuParser(); "arg3", "arg4", "--test", "-A", "opt3", }; options.addOption(option_A); options.addOption(option_r); options.addOption(option_S); options.addOption(option_test); commandLine = parser.parse(options, testArgs); if (commandLine.hasOption("A")) System.out.println(commandLine.getOptionValue("A")); if (commandLine.hasOption("r"))
Options options = new Options(); Option o = new Option("t", "table", true, "table to import into (must exist)"); o.setRequired(true); options.addOption(o); o = new Option("i", "input", true, "the directory in DFS to read files from"); o.setRequired(true); options.addOption(o); options.addOption("d", "debug", false, "switch on DEBUG log level"); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(NAME + " ", options, true); System.exit(-1); CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("d")) { Logger log = Logger.getLogger("mapreduce"); log.setLevel(Level.DEBUG);
@Override public T buildObject(String[] args, int offset, boolean printUsage, String usage) throws IOException { Options options = new Options(); options.addOption(HELP); options.addOption(USE_LOG); for (Option opt : getOptions().getOptions()) { options.addOption(opt); try { CommandLineParser parser = new DefaultParser(); cli = parser.parse(options, Arrays.copyOfRange(args, offset, args.length)); } catch (ParseException pe) { if (printUsage) { System.out.println("Command line parse exception: " + pe.getMessage()); printUsage(usage, options); if (cli.hasOption(HELP.getOpt())) { if (printUsage) { printUsage(usage, options); return buildObject(cli); } catch (Throwable t) { if (cli.hasOption(USE_LOG.getOpt())) { log.error("Failed to instantiate " + this.klazz.getName(), t); } else {
/** * Parse the user-specified options, get the generic options, and modify * configuration accordingly. * * @param opts Options to use for parsing args. * @param args User-specified arguments * @return true if the parse was successful */ private boolean parseGeneralOptions(Options opts, String[] args) throws IOException { opts = buildGeneralOptions(opts); CommandLineParser parser = new GnuParser(); boolean parsed = false; try { commandLine = parser.parse(opts, preProcessForWindows(args), true); processGeneralOptions(commandLine); parsed = true; } catch(ParseException e) { LOG.warn("options parsing failed: "+e.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("general options are: ", opts); } return parsed; }
@Test public void test11458() throws Exception Options options = new Options(); options.addOption( OptionBuilder.withValueSeparator( '=' ).hasArgs().create( 'D' ) ); options.addOption( OptionBuilder.withValueSeparator( ':' ).hasArgs().create( 'p' ) ); String[] args = new String[] { "-DJAVA_HOME=/opt/java" , "-pfile1:file2:file3" }; CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); String[] values = cmd.getOptionValues('D'); values = cmd.getOptionValues('p'); assertEquals(values[2], "file3"); Iterator<Option> iter = cmd.iterator(); while (iter.hasNext())
@Test public void testNegativeOption() throws Exception { String[] args = new String[] { "-b", "-1"} ; options.addOption("1", false, null); CommandLine cl = parser.parse(options, args); assertEquals("-1", cl.getOptionValue("b")); }
@Override public JmxTransConfiguration parseOptions(@Nonnull String[] args) throws OptionsException, org.apache.commons.cli.ParseException { CommandLineParser parser = new GnuParser(); CommandLine cl = parser.parse(getOptions(), args); Option[] options = cl.getOptions(); configuration.setAdditionalJars(option.getValuesList()); } else if (option.getOpt().equals("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar jmxtrans-all.jar", getOptions()); configuration.setHelp(true);
@Test public void testGetParsedOptionValue() throws Exception { Options options = new Options(); options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i")); options.addOption(OptionBuilder.hasArg().create("f")); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, new String[] { "-i", "123", "-f", "foo" }); assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue()); assertEquals("foo", cmd.getParsedOptionValue("f")); }
@Test public void testOptionGroupLong() throws Exception { OptionGroup group = new OptionGroup(); group.addOption(OptionBuilder.withLongOpt("foo").create()); group.addOption(OptionBuilder.withLongOpt("bar").create()); Options options = new Options(); options.addOptionGroup(group); CommandLine cl = parser.parse(options, new String[] { "--bar" }); assertTrue(cl.hasOption("bar")); assertEquals("selected option", "bar", group.getSelected()); }
public OptionsResult parseOptions(String[] args) { CommandLineParser parser = new BasicParser(); List<SparkMagicCommand.SparkOptionCommand> commands = new ArrayList<>(); try { CommandLine cmd = parser.parse(sparkOptions.getOptions(), args); if (cmd.hasOption(START)) { commands.add((sparkUI, parent) -> sparkMagicCommand.connectToSparkSession(sparkUI, parent)); } } catch (ParseException e) { return new ErrorOptionsResult(e.getMessage()); } return new SparkOptionsResult(commands); }
commandLine = parser.parse(allOptions, args, false); } catch (ParseException ex) { System.err.println(ex.getMessage()); usage(); return; if (commandLine.hasOption("-v") || commandLine.hasOption("--verbose")) { verbosity = Verbosity.VERBOSE; } else if (commandLine.hasOption("-q") || commandLine.hasOption("--quiet")) { verbosity = Verbosity.QUIET;
@Test public void testWorkaround1() throws Exception { CommandLineParser parser = new PosixParser(); String[] args = new String[]{ "-t-something" }; CommandLine commandLine = parser.parse(options, args); assertEquals("-something", commandLine.getOptionValue('t')); }