This little project provides a smaller converter from [http://commons.apache.org/proper/commons-cli/](Apache Commons CLI) commandline parser to the Typesafe Config.
First you generate your cli options as usual with commons-cli. For example
Options options = new Options(); Option debug = new Option("debug", "print debugging information"); Option help = new Option( "help", "print this message" ); options.addOption(debug); options.addOption(help);
For more information look at the usage page.
Your main method could now look like this
public static void main(String[] args) { CommandLineParser basicParser = new BasicParser(); CommandLine cmd = basicParser.parse(options, args); Config config = CommandLineConfig.fromCommandLine(cmd); }
Currently theres only one conversion strategy.
option-name { prop1 = val1 prop2 = val2 }
Example call: -D prop1=val1 -D prop2=val2
option-name { argName1 = val1 argName2 = val2 }
Example call: -position 23.6,12.4,1452,803
The options object is defined as follows. Note that the withArgName parameter must have the same structure as the actual commandline input. This includes separator and argument count.
Option position = OptionBuilder.withArgName("longitude,latitude,altitude,speed") // .hasArgs(4) // .withValueSeparator(',') // .withDescription("use four arguments for something") // .create("position");
Conversion will result in
position { longitude = 23.6 latitude = 12.4 altitude = 1452 speed = 803 }