For this homework, you will create a class to parse command-line arguments and store them in a map.
An argument is considered a flag if it starts with a -
dash character followed by at least 1 character that is not a whitespace or digit. All other arguments are considered values. A flag is only associated with a value if the value immediately follows the flag. Not all flags have values, not all values have associated flags, and values will be overwritten if there are repeated flags.
Avoid looping more often than necessary! For example, numFlags()
should not require a loop.
See the Javadoc comments in the template code for additional details.
Users of the search engine will use command-line arguments to indicate how it should launch, including what files to index, whether to produce file output, and eventually, how to launch the web server. Having a generalized and efficient way to process and store these command-line arguments will thus be important for all project milestones.
<aside> <img src="/icons/git_gray.svg" alt="/icons/git_gray.svg" width="40px" /> This homework assignment is directly useful for your project. Consider copying this class into your project repository when done!
</aside>
For example, consider the following command-line arguments:
"-max", "false", "-min", "0", "-min", "-10", "hello", "-@debug", "-f", "output.txt", "-verbose"
In this case, -max
-min
-@debug
-f
and -verbose
are all flags since they start with a -
dash followed by at least 1 character that is not a digit or whitespace. The values are false
0
hello
and output.txt
since they do not start with a -
dash character.
Note that -10
is not a flag because the -
dash is followed by a digit character. Instead it should be interpreted as a value representing a negative number.
Flag -max
has value false
. Flag -min
has initial value 0
, but the value get replaced by the second occurrence of the -min
flag with the value -10
instead. The value hello
has no associated flag and is ignored. The flags -@debug
and -verbose
have no associated value, but are still stored. The resulting map should look similar to:
{
"-max": "false",
"-min": "-10",
"-@debug": null,
"-f": "output.txt",
"-verbose": null
}
Below are some hints that may help with this homework assignment:
@see
tag in the Javadoc comments often give hints on which methods are useful for implementing that method.parse(...)
method is easier if you use a traditional for
loop instead of an enhanced for
loop.char
type. For example, use String.codePointAt(int)
method when trying to access an individual character.