How to create readable passing arguments? Java -
i have main class. want run @ command prompt , use arguments choose between 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 "+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 = 0; < 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 "+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 = 0; < 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 1st switch case)
so question how create "variable"(or other naming, not sure is.) in front of args?
example:
java -jar restclientnogui.jar /method="createorder" (for 1st switch case)
java -jar restclientnogui.jar /method="sendorder" /orderid="1234" (for 2nd switch case)
you use http://commons.apache.org/proper/commons-cli/. if want yourself, you'll parse command-line argument , set appropriate input based on prefix. this:
string input = args[0]; string[] keyval = input.split("="); string input1 = null; if(keyval[0].equals("method")){ input1 = keyval[1]; }
Comments
Post a Comment