import org.apache.commons.cli.*; public class MyCLI { public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "Display help information"); options.addOption("u", "username", true, "Username"); CommandLineParser parser = new DefaultParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { // Display help information HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("mycli", options); } else if (cmd.hasOption("u")) { // Process username option String username = cmd.getOptionValue("u"); System.out.println("Hello, " + username + "!"); } else { // No options provided System.out.println("No options provided. Use -h to display help information."); } } catch (ParseException e) { // Parsing failed, display error message System.err.println("Parsing failed. Use -h to display help information."); } } }


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