List of usage examples for org.apache.commons.cli Option hasArg
public boolean hasArg()
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 ava2s .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:de.fosd.jdime.config.CommandLineConfigSource.java
@Override protected Optional<String> getMapping(String key) { if (ARG_LIST.equals(key)) { List<String> argList = cmdLine.getArgList(); if (argList.isEmpty()) { return Optional.empty(); } else {/*from w ww.j a v a2 s . co m*/ return Optional.of(String.join(ARG_LIST_SEP, argList)); } } if (!options.hasOption(key)) { return Optional.empty(); } Option opt = options.getOption(key); String optName = opt.getOpt(); if (opt.hasArg()) { return Optional.ofNullable(cmdLine.getOptionValue(optName)); } else { return Optional.of(cmdLine.hasOption(optName) ? "true" : "false"); } }
From source file:com.googlecode.dex2jar.tools.BaseCmd.java
public void doMain(String... args) { initOptions();/*from w w w . ja v a 2s . co m*/ CommandLineParser parser = new PosixParser(); try { commandLine = parser.parse(options, args); } catch (ParseException ex) { usage(); return; } this.remainingArgs = commandLine.getArgs(); try { for (Option option : commandLine.getOptions()) { String opt = option.getOpt(); Field f = map.get(opt); if (f != null) { Object value; if (!option.hasArg()) {// no arg, it's a flag option value = true; } else { value = convert(commandLine.getOptionValue(opt), f.getType()); } f.set(this, value); } } doCommandLine(); } catch (Exception e) { e.printStackTrace(System.err); } }
From source file:com.netflix.exhibitor.standalone.ExhibitorCLI.java
private void logOptions(String sectionName, String prefix, Options options) { if (sectionName != null) { log.info("== " + sectionName + " =="); }//from w ww.ja va2 s .co m //noinspection unchecked for (Option option : (Iterable<? extends Option>) options.getOptions()) { if (option.hasLongOpt()) { if (option.hasArg()) { log.info(prefix + option.getLongOpt() + " <arg> - " + option.getDescription()); } else { log.info(prefix + option.getLongOpt() + " - " + option.getDescription()); } } } }
From source file:gobblin.runtime.cli.PublicMethodsCliObjectFactory.java
/** * For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains * that option, this method will automatically call the method on the input object with the correct * arguments./*from w ww . ja va 2s . c o m*/ */ public void applyCommandLineOptions(CommandLine cli, T embeddedGobblin) { try { for (Option option : cli.getOptions()) { if (!this.methodsMap.containsKey(option.getOpt())) { // Option added by cli driver itself. continue; } if (option.hasArg()) { this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue()); } else { this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin); } } } catch (IllegalAccessException | InvocationTargetException exc) { throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc); } }
From source file:com.bc.fiduceo.ingest.IngestionToolTest.java
@Test public void testGetOptions() { final Options options = IngestionTool.getOptions(); assertNotNull(options);//from www. jav a 2 s . c o m final Option helpOption = options.getOption("h"); assertNotNull(helpOption); assertEquals("h", helpOption.getOpt()); assertEquals("help", helpOption.getLongOpt()); assertEquals("Prints the tool usage.", helpOption.getDescription()); assertFalse(helpOption.hasArg()); final Option sensorOption = options.getOption("sensor"); assertNotNull(sensorOption); assertEquals("s", sensorOption.getOpt()); assertEquals("sensor", sensorOption.getLongOpt()); assertEquals("Defines the sensor to be ingested.", sensorOption.getDescription()); assertTrue(sensorOption.hasArg()); final Option configOption = options.getOption("config"); assertNotNull(configOption); assertEquals("c", configOption.getOpt()); assertEquals("config", configOption.getLongOpt()); assertEquals("Defines the configuration directory. Defaults to './config'.", configOption.getDescription()); assertTrue(configOption.hasArg()); final Option startTime = options.getOption("start-time"); assertNotNull(startTime); assertEquals("start", startTime.getOpt()); assertEquals("start-time", startTime.getLongOpt()); assertEquals("Define the starting time of products to inject.", startTime.getDescription()); assertTrue(startTime.hasArg()); final Option endTime = options.getOption("end-time"); assertNotNull(endTime); assertEquals("end", endTime.getOpt()); assertEquals("end-time", endTime.getLongOpt()); assertEquals("Define the ending time of products to inject.", endTime.getDescription()); assertTrue(endTime.hasArg()); final Option version = options.getOption("version"); assertNotNull(version); assertEquals("v", version.getOpt()); assertEquals("version", version.getLongOpt()); assertEquals("Define the sensor version.", version.getDescription()); assertTrue(version.hasArg()); }
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 w ww . j a va 2s . com*/ // 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:com.github.mrstampy.poisonivy.PoisonIvy.java
/** * Prints the help message to System.out. *//*from w ww . java2 s . com*/ protected void printHelpMessage() { out.println("Poison Ivy - Java Library Dependency Resolver and Application Launcher"); out.println(); out.println("Licence: GPL 2.0"); out.println("Copyright Burton Alexander 2014"); out.println(); out.println("Usage: "); out.println(); Options opts = getOptions(); for (Object op : opts.getOptions()) { Option o = (Option) op; out.println("-" + o.getOpt() + (o.hasArg() ? " [ARG]" : "") + " - " + o.getDescription()); } out.println(); out.println("The required options can be put into a file named '" + POISONIVY_CONFIG + "'"); out.println(); printExamples(); out.println(); out.println("See http://mrstampy.github.io/PoisonIvy/ for more information"); }
From source file:com.emc.vipr.sync.ViPRSync.java
/** * Loads and configures plugins based on command line options. *//* w ww. j a v a 2s . c om*/ protected static ViPRSync cliBootstrap(String[] args) throws ParseException { ViPRSync sync = new ViPRSync(); List<SyncPlugin> plugins = new ArrayList<>(); CommandLine line = gnuParser.parse(mainOptions(), args, true); // find a plugin that can read from the source String sourceUri = line.getOptionValue(SOURCE_OPTION); if (sourceUri != null) { for (SyncSource source : sourceLoader) { if (source.canHandleSource(sourceUri)) { source.setSourceUri(sourceUri); sync.setSource(source); plugins.add(source); LogMF.info(l4j, "source: {0} ({1})", source.getName(), source.getClass()); break; } } } String targetUri = line.getOptionValue(TARGET_OPTION); // find a plugin that can write to the target if (targetUri != null) { for (SyncTarget target : targetLoader) { if (target.canHandleTarget(targetUri)) { target.setTargetUri(targetUri); sync.setTarget(target); plugins.add(target); LogMF.info(l4j, "target: {0} ({1})", target.getName(), target.getClass()); break; } } } // load filters List<SyncFilter> filters = new ArrayList<>(); String filtersParameter = line.getOptionValue(FILTERS_OPTION); if (filtersParameter != null) { for (String filterName : filtersParameter.split(",")) { for (SyncFilter filter : filterLoader) { if (filter.getActivationName().equals(filterName)) { filters.add(filter); plugins.add(filter); LogMF.info(l4j, "filter: {0} ({1})", filter.getName(), filter.getClass()); break; } } } sync.setFilters(filters); } // configure thread counts if (line.hasOption(QUERY_THREADS_OPTION)) sync.setQueryThreadCount(Integer.parseInt(line.getOptionValue(QUERY_THREADS_OPTION))); if (line.hasOption(SYNC_THREADS_OPTION)) sync.setSyncThreadCount(Integer.parseInt(line.getOptionValue(SYNC_THREADS_OPTION))); // configure timings display if (line.hasOption(TIMINGS_OPTION)) sync.setTimingsEnabled(true); if (line.hasOption(TIMING_WINDOW_OPTION)) { sync.setTimingWindow(Integer.parseInt(line.getOptionValue(TIMING_WINDOW_OPTION))); } // configure recursive behavior if (line.hasOption(NON_RECURSIVE_OPTION)) sync.setRecursive(false); // configure failed object tracking if (line.hasOption(FORGET_FAILED_OPTION)) sync.setRememberFailed(false); // configure whether to delete source objects after they are successfully synced if (line.hasOption(DELETE_SOURCE_OPTION)) sync.setDeleteSource(true); // logging options if (line.hasOption(DEBUG_OPTION)) { sync.setLogLevel(DEBUG_OPTION); } if (line.hasOption(VERBOSE_OPTION)) { sync.setLogLevel(VERBOSE_OPTION); } if (line.hasOption(QUIET_OPTION)) { sync.setLogLevel(QUIET_OPTION); } if (line.hasOption(SILENT_OPTION)) { sync.setLogLevel(SILENT_OPTION); } // Quick check for no-args if (sync.getSource() == null) { throw new ConfigurationException("source must be specified"); } if (sync.getTarget() == null) { throw new ConfigurationException("target must be specified"); } // Let the plugins parse their own options // 1. add common options and all the options from the plugins Options options = mainOptions(); for (Object o : CommonOptions.getOptions().getOptions()) { options.addOption((Option) o); } for (SyncPlugin plugin : plugins) { for (Object o : plugin.getCustomOptions().getOptions()) { Option option = (Option) o; if (options.hasOption(option.getOpt())) { System.err.println( "WARNING: The option " + option.getOpt() + " is being used by more than one plugin"); } options.addOption(option); } } // 2. re-parse the command line based on these options line = gnuParser.parse(options, args); if (l4j.isDebugEnabled()) { for (Option option : line.getOptions()) { if (option.hasArg()) LogMF.debug(l4j, "parsed option {0}: {1}", option.getLongOpt(), line.getOptionValue(option.getLongOpt())); else LogMF.debug(l4j, "parsed option {0}", option.getLongOpt()); } } // 3. pass the result to each plugin separately for (SyncPlugin plugin : plugins) { plugin.parseOptions(line); } return sync; }
From source file:com.somerledsolutions.pa11y.client.cli.OptionsBuilderTest.java
private void assertTaskIdOption(Options options) { Option taskIdOption = options.getOption("tid"); assertEquals("tid", taskIdOption.getOpt()); assertEquals("Task ID", taskIdOption.getArgName()); assertEquals("taskid", taskIdOption.getLongOpt()); assertEquals("The ID of the task", taskIdOption.getDescription()); assertTrue(taskIdOption.hasArg()); }