假设我有一个简单的程序,它采用自变量输入以下列形式
DO1 inputLocation outputLocation
DO2 inputLocation outputLocation
DO3 [30或60或90] inputLocation outputLocation一个
DO4 [PD或C] inputLocation outputLocation
DO5 [GHI] inputLocation outputLocation如何处理命令行参数中的Java
我也有5个功能与程序相同的名称,我需要调用。到目前为止,我还以为做这种方式(在“半伪”)
static void main(String[] args)
{
if (args.length == 3)
processTriple(args);
if (args.length == 4)
processQuadruple(args);
throw new UnsupportedOperationException("dasdhklasdha");
}
在处理功能看起来像这样
processDouble(String args[])
{
String operation = "args[0]";
Location input = getInput(args[1]);
Location output = getInput(args[2]);
if (operation.equals("do1"))
do1(input,output);
if (operation.equals("do2"))
do2(input,output);
... etc
}
我做它似乎不是很可扩展的方式。如果一个函数的参数改变了,或者添加了新的函数,看起来这样做会很痛苦。 什么是“最好”的方式去这样的事情
可能重复(http://stackoverflow.com/问题/ 1200054/JAVA库换解析的命令行参数) –