dependencies { \timplementation 'commons-cli:commons-cli:1.4' } import org.apache.commons.cli.*; public class CommandLineApp { public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "Print help message"); options.addOption("v", "verbose", false, "Enable verbose mode"); options.addOption("f", "file", true, "Input file path"); CommandLineParser parser = new DefaultParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("command", options); } if (cmd.hasOption("v")) { System.out.println("Verbose mode enabled"); } if (cmd.hasOption("f")) { String filePath = cmd.getOptionValue("f"); System.out.println("Input file: " + filePath); } } catch (ParseException e) { System.out.println("Error parsing command line arguments: " + e.getMessage()); } } }


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