Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.verhas.utils;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  public class CommandLineProcessor {
  
 	private final Map<StringStringoptions = new HashMap<StringString>();
 	private final List<Stringfiles = new ArrayList<String>();

Get the options that were gathered by the method process().

Returns:
the map containing the command line argument key value pairs
 
 	public Map<StringStringgetOptions() {
 		return ;
 	}

Decides if a command line option was specified on the command line or not.

Should only be called after the method process() was invoked.

Parameters:
name the name of the option we are looking for
Returns:
true if the option was specified on the command line, and false if the option was not specified
 
 	public Boolean optionExists(String name) {
 		return getOptions().containsKey(name);
 	}

Get a command line option value.

Should only be called after the method process() was invoked.

Parameters:
name the name of the option we are looking for
Returns:
the value of the option
 
 	public String option(String name) {
 		return getOptions().get(name);
 	}

Get the non option (file) arguments that were gathered by the method process().

Returns:
the list of the arguments that do not have name in the order as they appeared on the command line
 
 	public List<StringgetFiles() {
 		return ;
 	}

Parses the command line to discover all the 'option' arguments and all the 'file' arguments. When an argument starts with double dash -- then this is a
 --arg=value
 
formatted argument. The value and the name of the option are separated using an = equal sign and thus present in the same command line argument.

When the argument starts with single dash - (and not double dash --) then it is a

 -arg value
 
type argument. The difference is that in this case the argument value is the next command line argument separated by space on the command line from the name of the options.

When an argument does not start with dash then this is a 'file' parameter and it gets into the files collection.

When a 'file' argument starts with a dash '-' then it has to be preceded with two dashes. For example -specialFile has to be written as ---specialFile.

The order of the parameters and the way they are specified ( -- or - ) is not preserved. The order of 'file' arguments is reserved in the array.

Parameters:
args the arguments passed to the main function
 
	public void process(final String[] args) {
		for (int i = 0; args != null && i < args.lengthi++) {
			String arg = args[i];
			if (arg.startsWith("---")) {
else if (arg.startsWith("--")) {
				arg = arg.substring(2);
				final int indexOfEqualSign = arg.indexOf("=");
				if (indexOfEqualSign == -1) {
					.put(argnull);
else {
					final String val = arg.substring(indexOfEqualSign + 1);
					arg = arg.substring(0, indexOfEqualSign);
					.put(argval);
				}
else if (arg.startsWith("-")) {
				arg = arg.substring(1);
				if (i + 1 < args.length) {
					.put(argargs[i + 1]);
else {
					.put(argnull);
				}
				i++;
else {
				.add(arg);
			}
		}
	}
New to GrepCode? Check out our FAQ X