import org.apache.commons.cli.*; public class MyCLIApp { public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "Display help information"); options.addOption("g", "greet", true, "Greet the specified name"); CommandLineParser parser = new DefaultParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { printHelp(options); } else if (cmd.hasOption("g")) { String name = cmd.getOptionValue("g"); System.out.println("Hello, " + name + "!"); } else { System.out.println("No command specified. Use -h or --help for more information."); } } catch (ParseException e) { System.out.println("Invalid command. Use -h or --help for more information."); } } private static void printHelp(Options options) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("mycliapp", options); } }


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