king jia
king jia

Reputation: 712

How to create readable passing arguments? Java

I have a main class. I want run it at command prompt and use arguments to choose between the switch cases.

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();
}

In CMD: java -jar RESTCLIENTNOGUI.jar "CREATEORDER" (To run the 1st switch case)

So my question is how to create a "variable"(or other naming, not sure what it is.) in front of the args?

Example:

java -jar RESTCLIENTNOGUI.jar /method="CREATEORDER" (for 1st switch case)

java -jar RESTCLIENTNOGUI.jar /method="SENDORDER" /OrderID="1234" (For 2nd switch case)

Upvotes: 0

Views: 157

Answers (1)

Harshal Pandya
Harshal Pandya

Reputation: 1624

You could use http://commons.apache.org/proper/commons-cli/. If you want to do it yourself, you'll could parse the command-line argument yourself and set the appropriate input based on the prefix. Something like this:

String input = args[0];
String[] keyVal = input.split("=");
String input1 = null;
If(keyVal[0].equals("method")){
     input1 = keyVal[1];
}

Upvotes: 1

Related Questions