You could use the refcodes-console artifact (see refcodes-console at Bitbucket):
Option<String> r = new StringOptionImpl( "-r", null, "opt1", "..." ); Option<String> s = new StringOptionImpl( "-S", null, "opt2", "..." ); Operand<String> arg1 = new StringOperandImpl( "arg1", "..." ); Operand<String> arg2 = new StringOperandImpl( "arg2", "..." ); Operand<String> arg3 = new StringOperandImpl( "arg3", "..." ); Operand<String> arg4 = new StringOperandImpl( "arg4", "..." ); Switch test = new SwitchImpl( null, "--test", "..." ); Option<String> a = new StringOptionImpl( "-A", null, "opt3", "..." ); Condition theRoot = new AndConditionImpl( r, s, a, arg1, arg2, arg3, arg4, test );
Create your arguments parser ArgsParserImpl with your root condition:
ArgsParser theArgsParser = new ArgsParserImpl( theRoot ); theArgsParser.setName( "MyProgramm" ); theArgsParser.setSyntaxNotation( SyntaxNotation.GNU_POSIX );
Above you define your syntax, below you invoke the parser:
theArgsParser.printUsage(); theArgsParser.printSeparatorLn(); theArgsParser.printOptions(); theArgsParser.evalArgs( new String[] { "-r", "RRRRR", "-S", "SSSSS", "11111", "22222", "33333", "44444", "--test", "-A", "AAAAA" } );
In case you provided some good descriptions, theArgsParser.printUsage() will even show you the pretty printed usage:
Usage: MyProgramm -r <opt1> -S <opt2> -A <opt3> arg1 arg2 arg3 arg4 --test
In case the --test switch is to be optional, assign theRoot as follows:
theRoot = new AndConditionImpl( r, s, a, arg1, arg2, arg3, arg4, new OptionalImpl( test ) );
Then your syntax looks as follows:
Usage: MyProgramm -r <opt1> -S <opt2> -A <opt3> arg1 arg2 arg3 arg4 [--test]
The full example for your case you find in the StackOverFlowExamle. You can use AND, OR, XOR conditions and any kind of nesting ... hope this helps.