0

我有一个主要课程。我想在命令提示符下运行它并使用参数在切换案例之间进行选择。

public static void main(String[] args) throws InterruptedException, IOException{

    String response = null;
    String input = args[0];

    switch (input) {
    case "CREATEORDER":
        String path1 = "order?customer_id=1";
        String body1 = file.getOrderXML("2");
        body1 = header + body1;
        response = HttpConnector.getInstance().execute("POST", path1, body1);
        System.out.println("Order ID is "+response);
        break;

    case "SENDORDER":
        String path2 = "cards?order_id=";
        String orderNumber2 = args[1];
        path2 += orderNumber2;
        String body2 = file.getCardXML("1", EMV, perso);
        response = HttpConnector.getInstance().execute("POST", path2, body2);
        System.out.println("Card Record: "+response);
        break;

    case "SENDORDERS":
        String path3 = "cards?order_id=";
        //"String" Order ID
        String orderNumber3 = args[1];
        path3 += orderNumber3;
        //"String" quantity
        String qtt = args[2];
        String body3 = file.getCardXML(qtt, EMV, perso);
        String[] lines = body3.split("\r\n|\r|\n");
        int number = lines.length;
        for(int i = 0; i < number ; i++){
            response = HttpConnector.getInstance().execute("POST", path3, lines[i]);
            System.out.println("Card Record: "+response);
        }
        break;

    case "COMBINED":
        String paths = "order?customer_id=1";
        String paths2 = "cards?order_id=";
        String response2 = "";
        //"String" Quantity
        String qtt2 = args[1];
        String body4 = file.getOrderXML(qtt2);
        body4 = header + body4;
        response = HttpConnector.getInstance().execute("POST", paths, body4);
        System.out.println("Order ID is "+response);
        Thread.sleep(2000);
        //Send Card Order
        String result2 = new getXMLFile().getCardXML(qtt2, EMV, perso);
        body4 = header + result2;
        String[] lines2 = body4.split("\r\n|\r|\n");
        int number2 = lines2.length;
        paths2 = paths2 + response.replace("\r\n", "");
        for(int i = 0; i < number2 ; i++){
            response2 += HttpConnector.getInstance().execute1(paths2,lines2[i]);
            System.out.println("Card Record: "+response2);
        }
        break;

    case "0":
        System.exit(0);
        break;
    }
    System.in.read();
}

在 CMD 中: java -jar RESTCLIENTNOGUI.jar "CREATEORDER" (运行第一个 switch 案例)

所以我的问题是如何在 args 前面创建一个“变量”(或其他命名,不确定它是什么。)?

例子:

java -jar RESTCLIENTNOGUI.jar /method="CREATEORDER" (对于第一个开关案例)

java -jar RESTCLIENTNOGUI.jar /method="SENDORDER" /OrderID="1234" (对于第二个开关案例)

4

1 回答 1

1

您可以使用http://commons.apache.org/proper/commons-cli/。如果你想自己做,你可以自己解析命令行参数并根据前缀设置适当的输入。像这样的东西:

String input = args[0];
String[] keyVal = input.split("=");
String input1 = null;
If(keyVal[0].equals("method")){
     input1 = keyVal[1];
}
于 2013-05-03T02:11:14.503 回答