Like @palacsint I will recommend an external library. Apache commons-cli is a decent choice. Another choice (my preference) is java gnu-getopt ... I like it because I am familiar with the notations and standards from previous work. It can be a little complicated the first time around otherwise.
On the other hand, I tend not to use an external library unless the code is already going to be relatively complicated....
But, back to your code.
Why do you have everything in a single String? Why is it not part of the
The first thing about command-line arguments is that they get complicated very fast. What if the argument was:
String command = "-ps 4 -pe 5 -E opInstallDir=/opt/OSS-EVAL/thiscode -E opWD=/home/paul/remake -E opFam=fam -E opAppli=appli -Edocs='My Documents' -Eparse=key=value";
I have thrown in a few things there.
First up, on our one machine at work, we really do have the directory /opt/OSS-EVAL/ which we use to install/evaluate OSS software/libraries.
The above will break your parsing because it has the
-E embedded in the name.
Next up, is 'POSIX-style' commandline arguments can have quoted values, and also values with an
= in the value.
So, things I would recommend to you:
Locate the source of your command-line values. It will likely be available as an array, not a single string. Keep the data as an array!
Second, with the array, it is easier to look for stand-alone values that are
-E, or, if the input is
-Ekey=value then you look for values that start with -E.
Finally, when you split the key/value on the
=, limit the split to 2.
String kv = pair.split("=", 2);
Which will preserve any of the
= tokens inside the value part.
You have suggested in your edit that this is for sending data to an external command.
If you are using Java to initialize the external command, then please, please, please use the version of exec() that takes a command array, or use the ProcessBuilder which allows you to send all the command-line parameters as separate values in an array!!!