For this homework assignment, create a class ArgumentParser that stores flag and value pairs in a map. Your class should be both generalized and encapsulated. Once complete, you'll end up using this class in all of your future projects!
Any command-line arguments passed in to your program may be accessed by the String[] args argument to the public static main method. For example, the following code prints out all of the command line arguments:
public class ArgumentDemo { public static void main(String[] args) { // print out number of arguments System.out.println("There are " + args.length + " arguments."); // print out all arguments for(int i = 0; i < args.length; i++) { System.out.println(i + " : " + args[i]); } } }
Notice that you receive an array of already parsed arguments. All you need to worry about is figuring out which ones are flags, and what value to associate with that flag. Consider the following example:
java Driver -a aaa -b bbb -c -d ddd -e
The array of arguments for this example would be:
{ "-a", "aaa", "-b", "bbb", "-c", "-d", "ddd", "-e" }
A flag is anything that starts with a dash – followed by one or more characters. In the above example, -a, -b, -c, -d, and -e are all flags.
Sometimes, there are values associated with flags. The value, if it exists, will always immediately follow the flag. In the above example, aaaa is associated with flag -a.
Some flags do not have values. For example, -c and -e do not have values since they are immediately followed by another flag, or are the last argument. If there is no value, then you should store a null value with the flag in your map.
The resulting map for this example would be:
"-a" → "aaa", "-b" → "bbb", "-c" → null, "-d" → "ddd", "-e" → null
You must decide which map implementation to use to store this information.
In addition to building this map, your class should have the following methods:
public ArgumentParser(String[] args) initializes the map based on the specified arguments using the parseArgs() helper method (see next)
private void parseArgs(String[] args) parses a String array of arguments into flag, value pairs and stores them in a map
public static boolean isFlag(String arg) tests whether or not an argument is considered a flag (i.e. it is a non-null string that starts with "–")
public static boolean isValue(String arg) tests whether or not an argument is considered a value (i.e. it is a non-null string that does not start with "–")
public boolean hasFlag(String flag) tests whether or not a particular flag was passed in as a command-line argument
public boolean hasValue(String flag) tests whether a particular flag both exists, and has a value associated with it
public String getValue(String flag) retrieves the value associated with a flag (may return null if no value exists)
public int numFlags() retrieves the number of flags (not arguments) that were passed in as command-line arguments
Your code must be properly encapsulated, protecting the integrity of your internal data structure. In other words, no other class should be able to directly manipulate the map storing flags and values through a passed reference.
You must submit your code via SVN to the following directory:
https://www.cs.usfca.edu/svn/username/cs212/homework02/
Replace username with your USF Connect username.
Your code must be committed by 11:59pm on Friday, February 8, 2013. Late homework is not accepted.
WARNING: Your code may be used in class as an example. All personally identificable information will be removed. Your identity will not be revealed.