CLA is a small Java library to parse parameters from command line or from a string.
CLA parameters are customizable and can be optionally executed using a CLA executor.
CLA requires JRE 8+
CLA library consists of three classes and an interface: CLAParser, Parameter, ParsedParameterMap, ParamExecutor (interface)
CLA example:
CLAParser testParser = new CLAParser(); // param -? testParser.addDefaultHelpParameter( System.out, "Test CLA, for Wiki", "?", "-" ); // param -version testParser.addParameter( new Parameter( "version", // name "-", "", // prefix, sufix "app version", // description false, // required 0, 0, // min-max cardinality null, // fixed values (p,v) -> { // executor System.out.println( CLAParser.LIBNAME + ", v" + CLAParser.VERSION ); System.exit( 0 ); }, Integer.MIN_VALUE + 1 // executor order ) ); // param -echo; testParser.addParameter( new Parameter( "echo", "-", ";", "echo values to console", false, 0, Integer.MAX_VALUE, null, (p,v) -> System.out.println( String.join( ",", v ) ), 0 ) ); // param -file testParser.addParameter( new Parameter( "file", "-", "", "load file", false, 1, 1, null, (p,v) -> System.out.println( "Loading file \"" + v.get( 0 ) + "\"" ), 10 ) ); // param -color testParser.addParameter( new Parameter( "color", "-", "", "set RGB color. Usage: -color R G B", false, 3, 3, null, (p,v) -> { int r = Integer.parseInt( v.get( 0 ) ); int g = Integer.parseInt( v.get( 1 ) ); int b = Integer.parseInt( v.get( 2 ) ); System.out.println( "Set color to R:" + r + ",G:" + g + ",B:" + b ); }, 20 ) ); try { testParser.parseAndExecute( "-?" ); } catch ( Exception e ) { e.printStackTrace(); }
Output:
==================================================
Help:
Test CLA, for Wiki
parameter [values{cardinality}] "description"
__________________________________________________
-? "Help about this command"
-color value{3} "set RGB color. Usage: -color R G B"
-echo; value{0-inf} "echo values by console"
-file value{1} "load file"
-version "app version"
==================================================
Output for testParser.parseAndExecute( "-color 100 5 255 -file \"hi all.txt\"" ); :
Loading file "hi all.txt"
Set color to R:100,G:5,B:255
Output for testParser.parseAndExecute( "-echo; p1 p2 p3 p4 -color 0 0 0" ); :
p1,p2,p3,p4
Set color to R:0,G:0,B:0