List of usage examples for org.apache.commons.cli Option getType
public Object getType()
From source file:de.mustnotbenamed.quickstart.undertowserver.CliHelper.java
public static <T> T option(CommandLine cmd, Option option, T defaultValue) throws ParseException { T result = defaultValue;/*from w w w . ja v a 2 s .c o m*/ if (cmd.hasOption(option.getOpt())) { String optionValue = cmd.getOptionValue(option.getOpt()); result = (T) TypeHandler.createValue(optionValue, option.getType()); } return result; }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.spi.TestCliOptionsBuilder.java
@Test public void testWithType() { Option option = this.builder.withType(23498723497L).create("test"); assertNotNull("The option should not be null.", option); assertEquals("getType is not correct.", 23498723497L, option.getType()); }
From source file:com.marklogic.contentpump.utilities.CommandlineOption.java
public CommandlineOption(Option opt) throws IllegalArgumentException { super(opt.getOpt(), opt.hasArg(), opt.getDescription()); this.setLongOpt(opt.getLongOpt()); this.setRequired(opt.isRequired()); this.setArgName(opt.getArgName()); this.setArgs(opt.getArgs()); this.setOptionalArg(opt.hasOptionalArg()); this.setType(opt.getType()); this.setValueSeparator(opt.getValueSeparator()); }
From source file:com.splunk.Command.java
public Command parse(String[] argv) { CommandLineParser parser = new PosixParser(); CommandLine cmdline = null;/* w ww . ja v a2 s . c o m*/ try { cmdline = parser.parse(this.rules, argv); } catch (ParseException e) { error(e.getMessage()); } // Unpack the cmdline into a simple Map of options and optionally // assign values to any corresponding fields found in the Command class. for (Option option : cmdline.getOptions()) { String name = option.getLongOpt(); Object value = option.getValue(); // Figure out the type of the option and convert the value. if (!option.hasArg()) { // If it has no arg, then its implicitly boolean and presence // of the argument indicates truth. value = true; } else { Class type = (Class) option.getType(); if (type == null) { // Null implies String, no conversion necessary } else if (type == Integer.class) { value = Integer.parseInt((String) value); } else { assert false; // Unsupported type } } this.opts.put(name, value); // Look for a field of the Command class (or subclass) that // matches the long name of the option and, if found, assign the // corresponding option value in order to provide simplified // access to command options. try { java.lang.reflect.Field field = this.getClass().getField(name); field.set(this, value); } catch (NoSuchFieldException e) { continue; } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } } String[] orig = this.args; String[] more = cmdline.getArgs(); this.args = new String[orig.length + more.length]; System.arraycopy(orig, 0, this.args, 0, orig.length); System.arraycopy(more, 0, this.args, orig.length, more.length); if (this.help) { printHelp(); System.exit(0); } return this; }
From source file:org.rhq.server.metrics.migrator.DataMigratorRunner.java
/** * Load the configuration options from file and overlay them on top of the default * options./* w w w. j a va2 s. c o m*/ * * @param file config file */ private void loadConfigFile(String file) { try { File configFile = new File(file); if (!configFile.exists()) { throw new FileNotFoundException("Configuration file not found!"); } Properties configProperties = new Properties(); FileInputStream stream = new FileInputStream(configFile); configProperties.load(stream); stream.close(); for (Object optionObject : options.getOptions()) { Option option = (Option) optionObject; Object optionValue; if ((optionValue = configProperties.get(option.getLongOpt())) != null) { log.debug("Configuration option loaded: " + option.getLongOpt() + " (" + option.getType() + ") -> " + optionValue); if (option.equals(cassandraHostsOption)) { String[] cassandraHosts = parseCassandraHosts(optionValue.toString()); configuration.put(option, cassandraHosts); } else if (option.equals(sqlServerTypeOption)) { if ("oracle".equals(optionValue)) { configuration.put(option, DatabaseType.Oracle); } else { configuration.put(option, DatabaseType.Postgres); } } else if (option.equals(sqlPostgresServerOption)) { boolean value = tryParseBoolean(optionValue.toString(), true); if (value == true) { configuration.put(sqlServerTypeOption, DatabaseType.Postgres); } } else if (option.equals(sqlOracleServerOption)) { boolean value = tryParseBoolean(optionValue.toString(), true); if (value == true) { configuration.put(sqlServerTypeOption, DatabaseType.Oracle); } } else if (option.getType().equals(Boolean.class)) { configuration.put(option, tryParseBoolean(optionValue.toString(), true)); } else if (option.getType().equals(Integer.class)) { configuration.put(option, tryParseInteger(optionValue.toString(), 0)); } else { configuration.put(option, optionValue.toString()); } } } } catch (Exception e) { log.error("Unable to load or process the configuration file.", e); System.exit(1); } }