import org.apache.commons.cli.*; public class FileTool { public static void main(String[] args) { Options options = new Options(); Option operation = new Option("c", "operation", true, "Operation to perform (copy, delete)"); operation.setRequired(true); options.addOption(operation); Option file = new Option("f", "file", true, "File path"); file.setRequired(true); options.addOption(file); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); try { CommandLine cmd = parser.parse(options, args); String operationValue = cmd.getOptionValue("operation"); String filePath = cmd.getOptionValue("file"); if (operationValue.equals("copy")) { System.out.println("Copying file: " + filePath); } else if (operationValue.equals("delete")) { System.out.println("Deleting file: " + filePath); } else { System.out.println("Invalid operation: " + operationValue); formatter.printHelp("FileTool", options); System.exit(1); } } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("FileTool", options); System.exit(1); } } }


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