Java Code Examples for org.apache.commons.cli.Option#getValue()

The following examples show how to use org.apache.commons.cli.Option#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CliUtil.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
/**
 * Displays all command line options in log messages.
 * @param line
 */
public static void logCommandLineOptions(Logger logger, CommandLine line) {
	for (Option myOption : line.getOptions()) {
		String message;
		String opt = "";
		if(myOption.getOpt() != null) {
			opt+="-"+myOption.getOpt();
			if(myOption.getLongOpt() != null) 
				opt+=" (--"+myOption.getLongOpt()+")";
		} else
			opt+="--"+myOption.getLongOpt()+"";
			
		if(myOption.hasArg()) 
		    message = opt + " " + myOption.getValue();
		else
			message = opt;
			
		
		logger.info("with option: " + message);
	}
}
 
Example 2
Source File: ConstructorAndPublicMethodsCliObjectFactory.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 3
Source File: ExecutePlatformCommandCommand.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private BlurObject createBlurObject(CommandLine commandLine, CommandDescriptor descriptor) throws CommandException {
  Map<String, ArgumentDescriptor> arguments = new TreeMap<String, ArgumentDescriptor>(
      descriptor.getOptionalArguments());
  arguments.putAll(descriptor.getRequiredArguments());
  BlurObject blurObject = new BlurObject();
  if (commandLine == null) {
    return null;
  }
  Option[] options = commandLine.getOptions();
  for (Option option : options) {
    String name = option.getOpt();
    String value = option.getValue();
    ArgumentDescriptor argumentDescriptor = arguments.get(name);
    String type = argumentDescriptor.getType();
    blurObject.put(name, convertIfNeeded(value, type));
  }
  return blurObject;
}
 
Example 4
Source File: AbstractAction.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
protected void checkOptions(Collection<Option> options) {
    for (Option option : options) {
        if (option.isRequired()) {
            String value = option.getValue();
            if (StringUtils.isBlank(value)) {
                throw new IllegalStateException("option: key =[" + option.getLongOpt() + "], required=["
                        + option.isRequired() + "] is blank!");
            }
        }
    }
}
 
Example 5
Source File: MetricTools.java    From cm_ext with Apache License 2.0 5 votes vote down vote up
private String getToolName() throws ParseException {
  for (Option option : cmdLine.getOptions()) {
    if (option.equals(OPT_TOOL)) {
      return option.getValue();
    }
  }
  // We should never get here as the parser of the command line would
  // have thrown an exception before for missing required argument.
  Preconditions.checkState(false);
  throw new ParseException("Metric tool name not found");
}
 
Example 6
Source File: AbstractUIDBuilder.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public void configure(final Configuration config, final Option... options) {
    if (null != config) {
        // Get the UID-specific options
        final Map<String,Option> uidOptions;
        if (null != options) {
            uidOptions = new HashMap<>(4);
            for (final Option option : options) {
                if (null != option) {
                    // Look for one of the 4 types of UID options
                    final String key = option.getLongOpt();
                    final String value;
                    if (UIDConstants.UID_TYPE_OPT.equals(key)) {
                        value = option.getValue(HashUID.class.getSimpleName());
                    } else if (UIDConstants.HOST_INDEX_OPT.equals(key)) {
                        value = option.getValue();
                    } else if (UIDConstants.PROCESS_INDEX_OPT.equals(key)) {
                        value = option.getValue();
                    } else if (UIDConstants.THREAD_INDEX_OPT.equals(key)) {
                        value = option.getValue();
                    } else {
                        value = null;
                    }
                    
                    // Check for null
                    if (null != value) {
                        // Put the key and value into the map
                        uidOptions.put(key, option);
                        
                        // Stop looping if we've got everything we need
                        if (uidOptions.size() >= 4) {
                            break;
                        } else if (UIDConstants.UID_TYPE_OPT.equals(key) && HashUID.class.getSimpleName().equals(value)) {
                            break;
                        }
                    }
                }
            }
        } else {
            uidOptions = Collections.emptyMap();
        }
        
        // Configure with the UID-specific options
        configure(config, uidOptions);
    }
}
 
Example 7
Source File: AbstractPropertyCommand.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public final R execute(final CommandLine commandLine) throws CommandException {
    try {
        final Properties properties = new Properties();

        // start by loading the properties file if it was specified
        if (commandLine.hasOption(CommandOption.PROPERTIES.getLongName())) {
            final String propertiesFile = commandLine.getOptionValue(CommandOption.PROPERTIES.getLongName());
            if (!StringUtils.isBlank(propertiesFile)) {
                try (final InputStream in = new FileInputStream(propertiesFile)) {
                    properties.load(in);
                }
            }
        } else {
            // no properties file was specified so see if there is anything in the session
            final SessionVariable sessionVariable = getPropertiesSessionVariable();
            if (sessionVariable != null) {
                final Session session = getContext().getSession();
                final String sessionPropsFiles = session.get(sessionVariable.getVariableName());
                if (!StringUtils.isBlank(sessionPropsFiles)) {
                    try (final InputStream in = new FileInputStream(sessionPropsFiles)) {
                        properties.load(in);
                    }
                }
            }
        }

        // add in anything specified on command line, and override anything that was already there
        for (final Option option : commandLine.getOptions()) {
            final String optValue = option.getValue() == null ? "" : option.getValue();
            properties.setProperty(option.getLongOpt(), optValue);
        }

        // delegate to sub-classes
        return doExecute(properties);

    } catch (CommandException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
    }
}