Command createCalculationCommand() { return new CommandBuilder() .withName("calculate") .withDescription("Perform a calculation") .addParameter(new ParameterBuilder() .withName("num1") .withDescription("First number") .withType(Integer.class) .build()) .addParameter(new ParameterBuilder() .withName("num2") .withDescription("Second number") .withType(Integer.class) .build()) .addParameter(new ParameterBuilder() .withName("operator") .withDescription("Operator (+, -, *, /)") .withType(String.class) .withValidator(Validator.fromList("+", "-", "*", "/")) .build()) .build(); } Prompt prompt = new Prompt(System.in, System.out); Parser parser = new DefaultParser(); ParsedCommand parsedCommand = parser.parse(prompt.getPrompt()); Command command = parsedCommand.getCommand(); Parameters parameters = parsedCommand.getParameters(); switch (command.getName()) { case "calculate": int num1 = parameters.getParameter("num1"); int num2 = parameters.getParameter("num2"); String operator = parameters.getParameter("operator"); int result = calculateResult(num1, num2, operator); System.out.println("Result: " + result); break; default: System.out.println("Unknown command: " + command.getName()); break; }


上一篇:
下一篇:
切换中文