import org.apache.commons.cli.*; public class CommandLineParserExample { public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "Display help information"); options.addOption("f", "file", true, "The file path"); CommandLineParser parser = new DefaultParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("MyProgram", options); return; } String filePath = cmd.getOptionValue("f"); System.out.println("File path: " + filePath); } catch (ParseException e) { System.out.println("Invalid command line arguments. Please see the help."); } } }


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