Options options = new Options(); Option input = new Option("i", "input", true, "input file path"); input.setRequired(true); options.addOption(input); Option output = new Option("o", "output", true, "output file"); output.setRequired(true); options.addOption(output); CommandLineParser parser = new DefaultParser(); 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");
.desc("path to target jar/aar file") .build()); options.addOption( Option.builder("p") .argName("pkg_name") .longOpt("package") .build()); options.addOption( Option.builder("o") .argName("out_path") .longOpt("output-file") Option.builder("v").argName("verbose").longOpt("verbose").desc("set verbosity").build()); try { CommandLine line = new DefaultParser().parse(options, args); if (line.hasOption('h')) { hf.printHelp(appName, options, true); return; String jarPath = line.getOptionValue('i'); String pkgName = line.getOptionValue('p', ""); String outPath = line.getOptionValue('o'); boolean debug = line.hasOption('d');
private void handleOption(Option option) throws ParseException { // check the previous option before handling the next one checkRequiredArgs(); option = (Option) option.clone(); updateRequiredOptions(option); cmd.addOption(option); if (option.hasArg()) { currentOption = option; } else { currentOption = null; } }
public static CommandLine parse(Options options, String[] args, boolean stopAtNonOptions) throws CliArgsException { final DefaultParser parser = new DefaultParser(); try { return parser.parse(options, args, stopAtNonOptions); } catch (ParseException e) { throw new CliArgsException(e.getMessage()); } }
/** * Parse command line options/flags and arguments. */ public CommandLine parseCommandLine(String[] args) throws ParseException { addOptions(); cmd = new DefaultParser().parse(this.options, args); return cmd; }
.build()); LoadMetricsServer.addCommandLineOptions(options); CommandLineParser parser = new DefaultParser(); CommandLine cmd = null; Exception commandLineException = null; if (cmd.hasOption("parallel")) { 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;
/** * Parse command line. */ public static CommandLine parseCommandLine(Options options, String[] args) throws ParseException { CommandLineParser parser = new DefaultParser(); CommandLine cli = parser.parse(options, args); if (cli.hasOption(StressTestUtils.HELP_OPT.getOpt())) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp( MRStressTest.class.getSimpleName(), OPTIONS); System.exit(0); } return cli; }
Options options = new Options(); options.addOption("build", false, "build model"); options.addOption("load", false, "load model"); options.addOption("save", false, "save model"); options.addOption("conf", true, "the path of configuration file"); options.addOption("D", true, "a specified key-value pair for configuration"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args, false); if (cmd.hasOption("conf")) { String confFilePath = cmd.getOptionValue("conf"); Properties prop = new Properties(); prop.load(new FileInputStream(confFilePath)); if (cmd.hasOption("jobconf")) { String[] optionValues = cmd.getOptionValues("jobconf"); for (String optionValue : optionValues) {
protected CommandLine doParse(String[] args) throws CommandLineParseException { CommandLineParser parser = new DefaultParser(); CommandLine commandLine; try { commandLine = parser.parse(options, args); if (commandLine.hasOption(HELP_ARG)) { return printUsageAndThrow(null, ExitCode.HELP); } postParse(commandLine); } catch (ParseException e) { return printUsageAndThrow("Error parsing command line. (" + e.getMessage() + ")", ExitCode.ERROR_PARSING_COMMAND_LINE); } return commandLine; }
@Override public void run(String[] args) { Options options = new Options(); options.addOption(HELP); options.addOption(ZK); options.addOption(JOB_NAME); options.addOption(ROOT_DIR); CommandLineParser parser = new DefaultParser(); cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length)); } catch (ParseException pe) { System.out.println( "Command line parse exception: " + pe.getMessage() ); return; if (cli.hasOption(HELP.getOpt())) { printUsage(options); return; if (!cli.hasOption(JOB_NAME.getOpt())) { log.error("Need Job Name to be specified --", JOB_NAME.getLongOpt()); throw new RuntimeException("Need Job Name to be specified"); } else { jobName = cli.getOptionValue(JOB_NAME.getOpt()); log.info("Using job name: {}", jobName);
/** * Parses the input args with a command line format, using * {@link org.apache.commons.cli.CommandLineParser}. * * @param args the input args * @return true if parsing succeeded */ private static boolean parseInputArgs(String[] args) { CommandLineParser parser = new DefaultParser(); CommandLine cmd; try { cmd = parser.parse(OPTIONS, args); } catch (ParseException e) { System.out.println("Failed to parse input args: " + e); return false; } sHelp = cmd.hasOption("help"); sMaster = cmd.getOptionValue("master", "FileSystemMaster"); sStart = Long.decode(cmd.getOptionValue("start", "0")); sEnd = Long.decode(cmd.getOptionValue("end", Long.valueOf(Long.MAX_VALUE).toString())); sJournalFile = cmd.getOptionValue("journalFile", ""); return true; }
public static void main(String[] args) { CommandLineParser parser = new DefaultParser(); Options options = new Options(); options.addOption(HELP).addOption(DEBUG).addOption(ALGORITHM).addOption(ITERATIONS); options.addOption(RESOURCE).addOption(PASSWORD).addOption(PASSWORD_NC); options.addOption(SALT).addOption(SALT_BYTES).addOption(SALT_GEN).addOption(SALT_GEN_SIZE).addOption(NO_SALT_GEN); CommandLine line = parser.parse(options, args); if (line.hasOption(HELP.getOpt())) { printHelpAndExit(options, null, debug, 0); if (line.hasOption(DEBUG.getOpt())) { debug = true; if (line.hasOption(ALGORITHM.getOpt())) { algorithm = line.getOptionValue(ALGORITHM.getOpt());
private String parseConfigLocation(String[] args) { Options options = new Options(); options.addOption(CLEANER_CONFIG); CommandLine cli; try { CommandLineParser parser = new DefaultParser(); cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length)); } catch (ParseException pe) { System.out.println("Command line parse exception: " + pe.getMessage()); printUsage(options); throw new RuntimeException(pe); } return cli.getOptionValue(CLEANER_CONFIG.getOpt()); }
@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); 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 {
public static void main(String[] args) throws Exception { LibrecTool tool = new DataDriver(); Options options = new Options(); options.addOption("build", false, "build model"); options.addOption("load", false, "load model"); options.addOption("save", false, "save model"); options.addOption("conf", true, "the path of configuration file"); options.addOption("jobconf", true, "a specified key-value pair for configuration"); options.addOption("D", true, "a specified key-value pair for configuration"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("build")) { tool.run(args); } else if (cmd.hasOption("load")) { } else if (cmd.hasOption("save")) { } }
Options options = new Options(); for (Opt opt : opts) { if (opt.noValue) { options.addOption(Option.builder(opt.shortName).longOpt(opt.longName).hasArg(false).build()); } else { options.addOption(Option.builder(opt.shortName).longOpt(opt.longName).hasArg().build()); DefaultParser parser = new DefaultParser(); CommandLine cl = parser.parse(options, rawArgs); HashMap<String, Object> ret = new HashMap<>(); for (Opt opt : opts) { if (opt.noValue) { ret.put(opt.shortName, cl.hasOption(opt.shortName)); } else { Object current = null; String[] strings = cl.getOptionValues(opt.shortName); if (strings != null) { for (String val : strings) { List<String> stringArgs = cl.getArgList(); if (args.size() > stringArgs.size()) { throw new RuntimeException("Wrong number of arguments at least " + args.size()
options.addOption("u", URL_OPTION, true, "NiFI URL to connect to (default: " + URL_OPTION_DEFAULT + ")"); options.addOption("d", DIRECTION_OPTION, true, "Direction (valid directions: " + Arrays.stream(TransferDirection.values()).map(Object::toString).collect(Collectors.joining(", ")) + ") (default: " + DIRECTION_OPTION_DEFAULT + ")"); options.addOption("n", PORT_NAME_OPTION, true, "Port name"); options.addOption("i", PORT_IDENTIFIER_OPTION, true, "Port id"); options.addOption(null, TIMEOUT_OPTION, true, "Timeout"); options.addOption(null, PROXY_PASSWORD_OPTION, true, "Proxy password"); options.addOption("h", HELP_OPTION, false, "Show help message and exit"); CommandLineParser parser = new DefaultParser(); CommandLine commandLine; commandLine = parser.parse(options, args); if (commandLine.hasOption(HELP_OPTION)) { printUsage(null, options); System.exit(1); builder.url(commandLine.getOptionValue(URL_OPTION, URL_OPTION_DEFAULT)); if (commandLine.hasOption(PORT_NAME_OPTION)) { builder.portName(commandLine.getOptionValue(PORT_NAME_OPTION)); builder.keystorePass(commandLine.getOptionValue(KEY_STORE_PASSWORD_OPTION)); } else { throw new ParseException("Must specify keystore password"); builder.truststorePass(commandLine.getOptionValue(TRUST_STORE_PASSWORD_OPTION)); } else { throw new ParseException("Must specify truststore password");
public static CliOptions parseGatewayModeGateway(String[] args) { try { DefaultParser parser = new DefaultParser(); CommandLine line = parser.parse(GATEWAY_MODE_GATEWAY_OPTIONS, args, true); return new CliOptions( line.hasOption(CliOptionsParser.OPTION_HELP.getOpt()), null, null, checkUrl(line, CliOptionsParser.OPTION_DEFAULTS), checkUrls(line, CliOptionsParser.OPTION_JAR), checkUrls(line, CliOptionsParser.OPTION_LIBRARY), null ); } catch (ParseException e) { throw new SqlClientException(e.getMessage()); } }
public static void main(String[] args) throws Exception { Options options = buildOptions(); try { CommandLine cmd = new DefaultParser().parse(options, args); if (!cmd.hasOption(ServiceConfigKeys.SERVICE_NAME_OPTION_NAME)) { printUsage(options); System.exit(1); } boolean isTestMode = false; if (cmd.hasOption("test_mode")) { isTestMode = Boolean.parseBoolean(cmd.getOptionValue("test_mode", "false")); } Config config = ConfigFactory.load(); try (GobblinServiceManager gobblinServiceManager = new GobblinServiceManager( cmd.getOptionValue(ServiceConfigKeys.SERVICE_NAME_OPTION_NAME), getServiceId(), config, Optional.<Path>absent())) { gobblinServiceManager.start(); if (isTestMode) { testGobblinService(gobblinServiceManager); } } } catch (ParseException pe) { printUsage(options); System.exit(1); } }
@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")); }