How to use terminal arguments with values in Java?

前端 未结 4 1674
长发绾君心
长发绾君心 2021-01-25 06:36

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

相关标签:
4条回答
  • 2021-01-25 07:04

    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 calling getNamed() 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 calling getUnnamed() 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 this List.

    0 讨论(0)
  • 2021-01-25 07:05

    If you are allowed to use external libraries, check out Apache Commons CLI, which will save you from re-inventing a wheel.

    0 讨论(0)
  • 2021-01-25 07:15

    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.

    0 讨论(0)
  • 2021-01-25 07:16

    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).

    0 讨论(0)
提交回复
热议问题