For my school project I am creating a game like Bad Apples for iPhone (not my personal choice but it isn\'t the problem).
The game needs to have two versions, the first
If you are using JavaFX you can use the getParameters
method in the Application class (please note, not in the constructor though), it handles both parameters from the command line as well from the jnlp file!
public final Application.Parameters getParameters()
Retrieves the parameters for this Application, including any arguments passed on the command line and any parameters specified in a JNLP file for an applet or WebStart application. NOTE: this method should not be called from the Application constructor, as it will return null. It may be called in the init() method or any time after that.
http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#getParameters()
For further details see the documentation for the returned object: http://docs.oracle.com/javafx/2/api/javafx/application/Application.Parameters.html
It handles both named and unnamed (and can always get the raw parameters of course).
Named parameters you get as a
Map
from callinggetNamed()
and include those pairs explicitly specified in a JNLP file. It also includes any command line arguments of the form: "--name=value"Unnamed parameters you get as a
List
by callinggetUnnamed()
Are the simple ones you already handle today in your code, meaning, that the named parameters, that is the parameters that are represented as pairs, are filtered out from thisList
.
If you are allowed to use external libraries, check out Apache Commons CLI, which will save you from re-inventing a wheel.
To answer the question you actually asked ...
Some of the elements of your args array are of the form "--SOMETHING=ANOTHER".
So, first thing you need is:
if(args[x].startsWith("--SOMETHING")) {
The second problem is to parse off the ANOTHER.
args[x].split("=")
is the place to start with that.
There is an interesting discussion of JavaFX and command line parameters in this blog post: Exploring JavaFX 2 - Accessing application parameters, in which the author recommends that you "let Application.getParameters() to act a transporter and ask Apache Commons CLI to take the main work" by which he means just get the raw parameter data rather than the parsed parameters:
getParameters().getRaw().toArray(new String[getParameters().getRaw().size()])
This translates the parameters to a JavaFX example into the same kind of String array you get from a command line app, and you can then handle the parameter parsing using a common function.
If you can't use Apache Commons CLI in your project, you could implement a basic parameter parser for your app yourself which handles in a common function the parsing based upon the parameters retrieved from a getParameters().getRaw().toArray
call (in JavaFX mode) or the main()
args passed to your app (in command line mode).