Reputation: 56912
My Groovy app has the following code:
def doSomething() {
def props = [
"fizz",
"buzz"
]
// Do something with 'props'...
}
Please note that props
could grow quite long (10+ elements) over time.
I now want to read props
from a switch at the command line (so, as System properties). So I'd like my code to be something like this:
def doSomething() {
def props = System.getProperty("props")
// Do something with 'props'...
}
So I could then run the app via:
java -Dprops=??? -jar myapp.jar
But now sure how to specify an array of Strings from the command line. Any ideas?
Upvotes: 4
Views: 1591
Reputation: 1
Have you thought about using a property file and using ConfigSlurper e.g. something like
File configFile = new File(configDir, 'config.groovy')
ConfigObject config = new ConfigSlurper().parse(configFile.toURI().toURL())
def props = config.props
where config.groovy looks like
props = ["fizz","buzz","etc"]
Or using CliBuilder to pass command line args
e.g. something like
CliBuilder cli = new CliBuilder(usage: "blah:")
cli.p(argName: 'prop', longOpt: 'prop', args: Option.UNLIMITED_VALUES, required: true, valueSeparator: ',', '')
def options = cli.parse(args)
def props = options.ps // ps gives you all the -p args
then
java -jar myapp.jar -p fizz,buzz,etc
or alternate allowed syntax
java -jar myapp.jar -p fizz -p buzz -p etc
Upvotes: 0
Reputation: 10707
As you said, there is no built in support but you can try this:
ArrayList props = System.getProperty("props").replaceAll("\\[","").replaceAll("]","").split(",")
and call it with:
java -Dprops="['fizz','buzz']" -jar myapp.jar
Upvotes: 3
Reputation: 56912
There doesn't seem to be built-in support for this, so I ended up going with:
System.getProperty("props").split(";")
Then from the command-line:
java -Dprops="fizz;buzz;etc" -jar myapp.jar
Upvotes: 3