public Option[] getOptions() { return commandLine.getOptions(); }
public static Properties commandLine2Properties(final CommandLine commandLine) { Properties properties = new Properties(); Option[] opts = commandLine.getOptions(); if (opts != null) { for (Option opt : opts) { String name = opt.getLongOpt(); String value = commandLine.getOptionValue(name); if (value != null) { properties.setProperty(name, value); } } } return properties; }
private boolean isYarnPropertiesFileMode(CommandLine commandLine) { boolean canApplyYarnProperties = !commandLine.hasOption(addressOption.getOpt()); if (canApplyYarnProperties) { for (Option option : commandLine.getOptions()) { if (allOptions.hasOption(option.getOpt())) { if (!isDetachedOption(option)) { // don't resume from properties file if yarn options have been specified canApplyYarnProperties = false; break; } } } } return canApplyYarnProperties; }
public String getOptionsAsString() { StringBuilder buf = new StringBuilder(); for (Option option : commandLine.getOptions()) { buf.append(" "); buf.append(option.getOpt()); if (option.hasArg()) { buf.append("="); buf.append(option.getValue()); } } return buf.toString(); }
/** * Builds an instance of T using the selected constructor getting the constructor * parameters from the {@link CommandLine}. * * Note: this method will also automatically call {@link #applyCommandLineOptions(CommandLine, T)} on * the constructed object. */ private T buildInstance(CommandLine cli) { String[] constructorArgs = new String[this.constructor.getParameterTypes().length]; for (Option option : cli.getOptions()) { if (this.constructoArgumentsMap.containsKey(option.getOpt())) { int idx = this.constructoArgumentsMap.get(option.getOpt()); constructorArgs[idx] = option.getValue(); } } T embeddedGobblin; try { embeddedGobblin = this.constructor.newInstance((Object[]) constructorArgs); return embeddedGobblin; } catch (IllegalAccessException | InvocationTargetException | InstantiationException exc) { throw new RuntimeException("Could not instantiate " + this.klazz.getName(), exc); } }
/** * 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. */ 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); } }
/** * Returns {@link ParameterTool} for the arguments parsed by {@link GenericOptionsParser}. * * @param args Input array arguments. It should be parsable by {@link GenericOptionsParser} * @return A {@link ParameterTool} * @throws IOException If arguments cannot be parsed by {@link GenericOptionsParser} * @see GenericOptionsParser */ public static ParameterTool paramsFromGenericOptionsParser(String[] args) throws IOException { Option[] options = new GenericOptionsParser(args).getCommandLine().getOptions(); Map<String, String> map = new HashMap<String, String>(); for (Option option : options) { String[] split = option.getValue().split("="); map.put(split[0], split[1]); } return ParameterTool.fromMap(map); } }
Map<ValidationTask.TaskResult, Integer> results = new HashMap<>(); Map<String, String> optionsMap = new HashMap<>(); for (Option opt : cmd.getOptions()) { optionsMap.put(opt.getOpt(), opt.getValue());
/** * Gets the worker info options. * * @param cl CommandLine that contains the client options * @return GetWorkerReportOptions to get worker information */ private GetWorkerReportOptions getOptions(CommandLine cl) throws IOException { if (cl.getOptions().length > 1) { System.out.println(getUsage()); throw new InvalidArgumentException("Too many arguments passed in."); } GetWorkerReportOptions workerOptions = GetWorkerReportOptions.defaults(); Set<WorkerInfoField> fieldRange = new HashSet<>(Arrays.asList(WorkerInfoField.ADDRESS, WorkerInfoField.WORKER_CAPACITY_BYTES, WorkerInfoField.WORKER_CAPACITY_BYTES_ON_TIERS, WorkerInfoField.LAST_CONTACT_SEC, WorkerInfoField.WORKER_USED_BYTES, WorkerInfoField.WORKER_USED_BYTES_ON_TIERS)); workerOptions.setFieldRange(fieldRange); if (cl.hasOption(ReportCommand.LIVE_OPTION_NAME)) { workerOptions.setWorkerRange(WorkerRange.LIVE); } else if (cl.hasOption(ReportCommand.LOST_OPTION_NAME)) { workerOptions.setWorkerRange(WorkerRange.LOST); } else if (cl.hasOption(ReportCommand.SPECIFIED_OPTION_NAME)) { workerOptions.setWorkerRange(WorkerRange.SPECIFIED); String addressString = cl.getOptionValue(ReportCommand.SPECIFIED_OPTION_NAME); String[] addressArray = addressString.split(","); // Addresses in GetWorkerReportOptions is only used when WorkerRange is SPECIFIED workerOptions.setAddresses(new HashSet<>(Arrays.asList(addressArray))); } return workerOptions; }
@Override public int run(CommandLine cl) throws AlluxioException, IOException { if (cl.getOptions().length > 1) { return -1;
for ( Option opt : mavenArgs.getOptions() ) for ( Option opt : mavenConfig.getOptions() )
CommandLine cmdline = parser.parse(options, args, false); List<String> cmdlineArgs = cmdline.getArgList(); Option [] cmdlineOptions = cmdline.getOptions(); HelpFormatter formatter = new HelpFormatter();
if (!cl.getOptions().isEmpty()) {
CommandLineParser parser = new GnuParser(); CommandLine cl = parser.parse(getOptions(), args); Option[] options = cl.getOptions();
for (final Option cliOptionSpecified : cliCommandLine.getOptions()) { final OptionDefinition optionDefinitionSpecified = this.optionDefinitionFromCliOption.get(cliOptionSpecified); for (final Option cliOptionSpecified : cliCommandLine.getOptions()) { final OptionDefinition optionDefinitionSpecified = this.optionDefinitionFromCliOption.get(cliOptionSpecified);
@Test public void testGetOptions() { CommandLine cmd = new CommandLine(); assertNotNull(cmd.getOptions()); assertEquals(0, cmd.getOptions().length); cmd.addOption(new Option("a", null)); cmd.addOption(new Option("b", null)); cmd.addOption(new Option("c", null)); assertEquals(3, cmd.getOptions().length); }
boolean forceActive = cmd.hasOption(FORCEACTIVE); int numOpts = cmd.getOptions() == null ? 0 : cmd.getOptions().length; final String[] args = cmd.getArgs();
for (final Option option : commandLine.getOptions()) { final String optValue = option.getValue() == null ? "" : option.getValue(); properties.setProperty(option.getLongOpt(), optValue);
if (cl.getOptions().length > 0) { throw new InvalidArgumentException( String.format("report %s does not support arguments: %s", command.toString().toLowerCase(), cl.getOptions()[0].getOpt()));
@Test public void testBuilder() throws Exception { CommandLine.Builder builder = new CommandLine.Builder(); builder.addArg( "foo" ).addArg( "bar" ); builder.addOption( Option.builder( "T" ).build() ); CommandLine cmd = builder.build(); assertEquals( "foo", cmd.getArgs()[0] ); assertEquals( "bar", cmd.getArgList().get( 1 ) ); assertEquals( "T", cmd.getOptions()[0].getOpt() ); } }