List of usage examples for org.apache.commons.cli Option hasArgs
public boolean hasArgs()
From source file:com.github.horrorho.inflatabledonkey.args.ArgsManager.java
String parse(Option option, UnaryOperator<String> parser) { if (option.hasArgs()) { return option.getValuesList().stream().map(u -> parse(option.getLongOpt(), u, parser)) .collect(Collectors.joining(" ")); }//w ww. ja va2 s. c o m if (option.hasArg()) { return parse(option.getLongOpt(), option.getValue(), parser); } return Boolean.TRUE.toString(); }
From source file:com.github.horrorho.inflatabledonkey.args.PropertyLoader.java
String parse(Option option) { testIntegers(option);//w w w. java2 s .co m if (option.hasArgs()) { // Array return option.getValuesList().stream().collect(Collectors.joining(" ")); } if (option.hasArg()) { // Value return option.getValue(); } // Boolean return Boolean.TRUE.toString(); }
From source file:com.github.horrorho.liquiddonkey.settings.commandline.CommandLinePropertiesFactory.java
public Properties from(Properties parent, CommandLineOptions commandLineOptions, String[] args) throws ParseException { Properties properties = new Properties(parent); CommandLineParser parser = new DefaultParser(); Options options = commandLineOptions.options(); CommandLine cmd = parser.parse(options, args); switch (cmd.getArgList().size()) { case 0://from ww w . j a v a 2 s . c o m // No authentication credentials break; case 1: // Authentication token properties.put(Property.AUTHENTICATION_TOKEN.name(), cmd.getArgList().get(0)); break; case 2: // AppleId/ password pair properties.put(Property.AUTHENTICATION_APPLEID.name(), cmd.getArgList().get(0)); properties.put(Property.AUTHENTICATION_PASSWORD.name(), cmd.getArgList().get(1)); break; default: throw new ParseException( "Too many non-optional arguments, expected appleid/ password or authentication token only."); } Iterator<Option> it = cmd.iterator(); while (it.hasNext()) { Option option = it.next(); String opt = commandLineOptions.opt(option); String property = commandLineOptions.property(option).name(); if (option.hasArgs()) { // String array properties.put(property, joined(cmd.getOptionValues(opt))); } else if (option.hasArg()) { // String value properties.put(property, cmd.getOptionValue(opt)); } else { // String boolean properties.put(property, Boolean.toString(cmd.hasOption(opt))); } } return properties; }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.spi.TestCliOptionsBuilder.java
@Test public void testHasArgs() { Option option = this.builder.hasArgs().create("test"); assertNotNull("The option should not be null.", option); assertTrue("hasArgs should be true.", option.hasArgs()); assertEquals("getArgs is not correct.", -2, option.getArgs()); }
From source file:com.axelor.shell.core.Target.java
public Object[] findArguments(String[] args) { final List<Object> arguments = new ArrayList<>(method.getParameterTypes().length); final Options options = getOptions(); final CommandLineParser lineParser = new BasicParser(); final CommandLine cmdLine; try {//from w ww . j a va 2 s . com cmdLine = lineParser.parse(options, args); } catch (ParseException e) { System.out.println(); System.out.println(e.getMessage()); System.out.println(); return null; } for (CliOption cliOption : cliOptions) { if (cliOption == null) { arguments.add(cmdLine.getArgs()); continue; } String key = "" + cliOption.shortName(); if (isBlank(key)) { key = cliOption.name(); } Option opt = options.getOption(key); Object value = false; if (opt.hasArgs()) { value = cmdLine.getOptionValues(key); } else if (opt.hasArg()) { value = cmdLine.getOptionValue(key); } else { value = cmdLine.hasOption(key); } arguments.add(value); } return arguments.toArray(); }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.spi.TestCliOptionsBuilder.java
@Test public void testHasArgsIntArg() { Option option = this.builder.hasArgs(5).create("test"); assertNotNull("The option should not be null.", option); assertTrue("hasArgs should be true.", option.hasArgs()); assertEquals("getArgs is not correct.", 5, option.getArgs()); }
From source file:edu.harvard.med.screensaver.io.CommandLineApplication.java
public void addCommandLineOption(Option option, Object defaultValue) { _options.addOption(option);/*from ww w. ja v a 2 s. c om*/ if (option.hasArgs()) { if (defaultValue instanceof List) { throw new IllegalArgumentException("when option takes multiple args, defaultValue must be a List"); } } _option2DefaultValue.put(option.getOpt(), defaultValue); }
From source file:de.rrze.idmone.utils.jpwgen.PwGenerator.java
/** * Initializes the CLI (Command Line Interface) options of the PwGenerator. * /* ww w. ja va2 s .com*/ * @return the CLI options */ private synchronized static Options createOptions() { options = new Options(); Option option = createOption(CL_NUMBER_PASSWORD, CL_NUMBER_PASSWORD_LONG, CL_NUMBER_PASSWORD_DESC, true, false); options.addOption(option); option = createOption(CL_PASSWORD_LENGTH, CL_PASSWORD_LENGTH_LONG, CL_PASSWORD_LENGTH_DESC, true, false); options.addOption(option); option = createOption(CL_CAPITALIZE, CL_CAPITALIZE_LONG, CL_CAPITALIZE_DESC, false, false); options.addOption(option); option = createOption(CL_NO_CAPITALIZE, CL_NO_CAPITALIZE_LONG, CL_NO_CAPITALIZE_DESC, false, false); options.addOption(option); option = createOption(CL_NUMERALS, CL_NUMERALS_LONG, CL_NUMERALS_DESC, false, false); options.addOption(option); option = createOption(CL_NO_NUMERALS, CL_NO_NUMERALS_LONG, CL_NO_NUMERALS_DESC, false, false); options.addOption(option); option = createOption(CL_SYMBOLS, CL_SYMBOLS_LONG, CL_SYMBOLS_DESC, false, false); options.addOption(option); option = createOption(CL_SYMBOLS_REDUCED, CL_SYMBOLS_REDUCED_LONG, CL_SYMBOLS_REDUCED_DESC, false, false); options.addOption(option); option = createOption(CL_NO_SYMBOLS, CL_NO_SYMBOLS_LONG, CL_NO_SYMBOLS_DESC, false, false); options.addOption(option); option = createOption(CL_AMBIGOUS, CL_AMBIGOUS_LONG, CL_AMBIGOUS_DESC, false, false); options.addOption(option); option = createOption(CL_NO_AMBIGOUS, CL_NO_AMBIGOUS_LONG, CL_NO_AMBIGOUS_DESC, false, false); options.addOption(option); option = createOption(CL_HELP, CL_HELP_LONG, CL_HELP_DESC, false, false); options.addOption(option); option = createOption(CL_RANDOM, CL_RANDOM_LONG, CL_RANDOM_DESC, false, false); options.addOption(option); option = createOption(CL_COLUMN, CL_COLUMN_LONG, CL_COLUMN_DESC, false, false); options.addOption(option); option = createOption(CL_SR_PROVIDERS, CL_SR_PROVIDERS_LONG, CL_SR_PROVIDERS_DESC, false, false); options.addOption(option); option = createOption(CL_PROVIDERS, CL_PROVIDERS_LONG, CL_PROVIDERS_DESC, false, false); options.addOption(option); option = createOption(CL_SR_ALGORITHM, CL_SR_ALGORITHM_LONG, CL_SR_ALGORITHM_LONG, true, false); option.hasArgs(); option.setArgs(2); options.addOption(option); option = createOption(CL_TERM_WIDTH, CL_TERM_WIDTH_LONG, CL_TERM_WIDTH_DESC, true, false); options.addOption(option); option = createOption(CL_MAX_ATTEMPTS, CL_MAX_ATTEMPTS_LONG, CL_MAX_ATTEMPTS_DESC, true, false); options.addOption(option); // regex option = createOption(CL_REGEX_STARTS_NO_SMALL_LETTER, CL_REGEX_STARTS_NO_SMALL_LETTER_LONG, CL_REGEX_STARTS_NO_SMALL_LETTER_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ENDS_NO_SMALL_LETTER, CL_REGEX_ENDS_NO_SMALL_LETTER_LONG, CL_REGEX_ENDS_NO_SMALL_LETTER_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_STARTS_NO_UPPER_LETTER, CL_REGEX_STARTS_NO_UPPER_LETTER_LONG, CL_REGEX_STARTS_NO_UPPER_LETTER_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ENDS_NO_UPPER_LETTER, CL_REGEX_ENDS_NO_UPPER_LETTER_LONG, CL_REGEX_ENDS_NO_UPPER_LETTER_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ENDS_NO_DIGIT, CL_REGEX_ENDS_NO_DIGIT_LONG, CL_REGEX_ENDS_NO_DIGIT_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_STARTS_NO_DIGIT, CL_REGEX_STARTS_NO_DIGIT_LONG, CL_REGEX_STARTS_NO_DIGIT_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_STARTS_NO_SYMBOL, CL_REGEX_STARTS_NO_SYMBOL_LONG, CL_REGEX_STARTS_NO_SYMBOL_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ENDS_NO_SYMBOL, CL_REGEX_ENDS_NO_SYMBOL_LONG, CL_REGEX_ENDS_NO_SYMBOL_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ONLY_1_CAPITAL, CL_REGEX_ONLY_1_CAPITAL_LONG, CL_REGEX_ONLY_1_CAPITAL_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ONLY_1_SYMBOL, CL_REGEX_ONLY_1_SYMBOL_LONG, CL_REGEX_ONLY_1_SYMBOL_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_AT_LEAST_2_SYMBOLS, CL_REGEX_AT_LEAST_2_SYMBOLS_LONG, CL_REGEX_AT_LEAST_2_SYMBOLS_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_ONLY_1_DIGIT, CL_REGEX_ONLY_1_DIGIT_LONG, CL_REGEX_ONLY_1_DIGIT_DESC, false, false); options.addOption(option); option = createOption(CL_REGEX_AT_LEAST_2_DIGITS, CL_REGEX_AT_LEAST_2_DIGITS_LONG, CL_REGEX_AT_LEAST_2_DIGITS_DESC, false, false); options.addOption(option); return options; }
From source file:org.apache.flink.table.client.config.Deployment.java
/** * Parses the given command line options from the deployment properties. Ignores properties * that are not defined by options./*from ww w. ja va 2 s .c om*/ */ public CommandLine getCommandLine(Options commandLineOptions) throws Exception { final List<String> args = new ArrayList<>(); properties.forEach((k, v) -> { // only add supported options if (commandLineOptions.hasOption(k)) { final Option o = commandLineOptions.getOption(k); final String argument = "--" + o.getLongOpt(); // options without args if (!o.hasArg()) { final Boolean flag = Boolean.parseBoolean(v); // add key only if (flag) { args.add(argument); } } // add key and value else if (!o.hasArgs()) { args.add(argument); args.add(v); } // options with multiple args are not supported yet else { throw new IllegalArgumentException("Option '" + o + "' is not supported yet."); } } }); return CliFrontendParser.parse(commandLineOptions, args.toArray(new String[args.size()]), true); }
From source file:org.apache.flink.table.client.config.entries.DeploymentEntry.java
/** * Parses the given command line options from the deployment properties. Ignores properties * that are not defined by options./*from w w w. ja va2s. c om*/ */ public CommandLine getCommandLine(Options commandLineOptions) throws Exception { final List<String> args = new ArrayList<>(); properties.asMap().forEach((k, v) -> { // only add supported options if (commandLineOptions.hasOption(k)) { final Option o = commandLineOptions.getOption(k); final String argument = "--" + o.getLongOpt(); // options without args if (!o.hasArg()) { final Boolean flag = Boolean.parseBoolean(v); // add key only if (flag) { args.add(argument); } } // add key and value else if (!o.hasArgs()) { args.add(argument); args.add(v); } // options with multiple args are not supported yet else { throw new IllegalArgumentException("Option '" + o + "' is not supported yet."); } } }); return CliFrontendParser.parse(commandLineOptions, args.toArray(new String[args.size()]), true); }