Search code examples
javagradlegetopt

How use GetOpt and LongOpt with gradle?


In my project, based on Gradle, i need parse command line and get option "-foo=...". I try to use Getopt, but i can't understand some things:

I need to use LongOpt to read "-foo" from command line ? How can i pass arguments to command line, with "gradle run" command ?

p.s. sorry for my bad english


Solution

  • gradle run is intended to launch an application without any arguments. You can hack around that if your arguments are pretty simple.

    e.g.:

    apply plugin: 'application'
    
    run {
       if (project.hasProperty("runArgs")) {
          args project.runArgs.split()
       }
    }
    

    Run it with gradle run -PrunArgs="arg1 arg2 arg3"

    This is pretty cumbersome to type and if your arguments need to be escaped or have spaces, it's going to be harder to handle. At that point, I'd recommend just running gradle installApp and then the appropriate start script (build/install/<app>/bin/<app> arg1 arg2 arg3).

    You can of course do something more elaborate: http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run