import org.apache.commons.cli.*; public class MyCLI { public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "Print help message"); 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("mycli", options); } String filePath = cmd.getOptionValue("f"); } catch (ParseException e) { System.err.println("Error parsing command line options: " + e.getMessage()); } } } import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; @Command(name = "mycli", mixinStandardHelpOptions = true, version = "1.0", description = "My CLI Tool") public class MyCLI implements Runnable { @Option(names = {"-f", "--file"}, description = "Input file path") private String filePath; @Override public void run() { } public static void main(String[] args) { CommandLine.run(new MyCLI(), args); } } import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; public class MyCLI { @Parameter(names = {"-f", "--file"}, description = "Input file path") private String filePath; public static void main(String[] args) { MyCLI cli = new MyCLI(); JCommander commander = JCommander.newBuilder() .addObject(cli) .build(); commander.parse(args); } }


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