你可以在REFCODES在refcodes-console使用refcodes-console
神器。ORG:
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);
与根的条件创建参数解析器ArgsParserImpl
:
ArgsParser theArgsParser = new ArgsParserImpl(theRoot);
theArgsParser.setName("MyProgramm");
theArgsParser.setSyntaxNotation(SyntaxNotation.GNU_POSIX);
以上定义你的语法,下面你调用解析器:
theArgsParser.printUsage();
theArgsParser.printSeparatorLn();
theArgsParser.printOptions();
theArgsParser.evalArgs(new String[] {
"-r", "RRRRR", "-S", "SSSSS", "11111", "22222", "33333", "44444",
"--test", "-A", "AAAAA"
});
如果你提供了一些很好的描述,theArgsParser.printUsage()
甚至会告诉你漂亮的印刷用法:
Usage: MyProgramm -r <opt1> -S <opt2> -A <opt3> arg1 arg2 arg3 arg4 --test
在上述例子中所有定义参数必须由用户通过,否则所述解析器将检测到错误的使用。万一--test
开关是为可选(或任何其它参数),分配theRoot
如下:
theRoot =新AndConditionImpl(R,S,A,ARG1,ARG2,ARG3,ARG4,新OptionalImpl(test));
那么你的语法如下所示:
Usage: MyProgramm -r <opt1> -S <opt2> -A <opt3> arg1 arg2 arg3 arg4 [--test]
您的情况下,完整的例子,你在StackOverFlowExamle找到。你可以使用AND,OR,XOR条件和任何类型的嵌套...希望这有助于。
评估解析参数如下所示:r.getValue());
或if (test.getValue() == true) ...
:
LOGGER.info("r :=" + r.getValue());
LOGGER.info("S :=" + s.getValue());
LOGGER.info("arg1 :=" + arg1.getValue());
LOGGER.info("arg2 :=" + arg2.getValue());
LOGGER.info("arg3 :=" + arg3.getValue());
LOGGER.info("arg4 :=" + arg4.getValue());
LOGGER.info("test :=" + test.getValue() + "");
LOGGER.info("A :=" + a.getValue());
的可能的复制(http://stackoverflow.com/questions/367706/how-to-parse-command- line-arguments-in-java) – xenteros