Created
July 28, 2011 10:39
-
-
Save icoloma/1111355 to your computer and use it in GitHub Desktop.
Java main arguments parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import com.google.common.collect.Lists; | |
import com.google.common.collect.Maps; | |
import com.google.common.collect.Sets; | |
/** | |
* Parses options and arguments provided in the command line. All option values must be preceded with "--" | |
* Example of use: | |
* | |
* MainArguments margs = new MainArguments(new String[] { "--port", "--user" }, args); | |
* String port = margs.getOptionAsString("--port", "8080"); | |
* String user = margs.getOptionAsString("--user", "foobar"); | |
* | |
* @author icoloma | |
* | |
*/ | |
public class MainArguments { | |
/** Map of options provided in the command line */ | |
private static Map<String, String> options = Maps.newHashMap(); | |
/** List with all remaining command line args */ | |
private List<String> remainderArgs = Lists.newArrayList(); | |
/** | |
* @param validOptions the list of options that are supported by this aplication | |
* @param cmdArgs the array of arguments passed by the command line | |
*/ | |
public MainArguments(String[] validOptions, String[] cmdArgs) { | |
Set<String> vo = Sets.newHashSet(validOptions); | |
for (int i = 0; i < cmdArgs.length; ) { | |
String arg = cmdArgs[i++]; | |
if (arg.startsWith("-")) { | |
if (!vo.contains(arg)) { | |
throw new IllegalArgumentException("Unexpected option: " + arg); | |
} | |
if (i >= cmdArgs.length) { | |
throw new IllegalArgumentException("Missing value for " + arg); | |
} | |
options.put(arg, cmdArgs[i++]); | |
} else { | |
remainderArgs.add(arg); | |
} | |
} | |
} | |
/** | |
* Return the value of the provided option | |
* @param name the name of the option | |
* @param defaultValue the value to return if null | |
*/ | |
public String getOptionAsString(String name, String defaultValue) { | |
String value = options.get(name); | |
return value == null? defaultValue : value; | |
} | |
public Integer getOptionAsInteger(String name, Integer defaultValue) { | |
String value = options.get(name); | |
return value == null? defaultValue : Integer.parseInt(value, 10); | |
} | |
public List<String> getRemainderArgs() { | |
return remainderArgs; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import org.junit.Test; | |
public class MainArgumentsTest { | |
@Test | |
public void testParseOptions() { | |
MainArguments args = new MainArguments( | |
new String[] { "--foo", "--bar" }, | |
new String[] { "--foo", "fooValue", "bazziebazbaz" } | |
); | |
assertEquals("fooValue", args.getOptionAsString("--foo", "xxx")); | |
assertEquals("barDefaults", args.getOptionAsString("--bar", "barDefaults")); | |
assertEquals(1, args.getRemainderArgs().size()); | |
assertEquals("bazziebazbaz", args.getRemainderArgs().get(0)); | |
} | |
@Test(expected=IllegalArgumentException.class) | |
public void testMissingOption() { | |
new MainArguments( | |
new String[] { "--foo", "--bar" }, | |
new String[] { "--foo" } | |
); | |
} | |
@Test(expected=IllegalArgumentException.class) | |
public void testIllegalOption() { | |
new MainArguments( | |
new String[] { "--foo", "--bar" }, | |
new String[] { "--xxx" } | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment