Options options = new Options(); Option inputOption = new Option("i", "input", true, "input file"); options.addOption(inputOption); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); String inputFile = cmd.getOptionValue("i"); Option sizeOption = new Option("s", "size", true, "size of the file"); sizeOption.setType(Integer.class); sizeOption.setRequired(true); sizeOption.setValidator(new NumericValidator()); options.addOption(sizeOption); import org.apache.commons.cli.*; public class CommandLineApp { public static void main(String[] args) { Options options = new Options(); Option inputOption = new Option("i", "input", true, "input file"); options.addOption(inputOption); CommandLineParser parser = new DefaultParser(); try { CommandLine cmd = parser.parse(options, args); String inputFile = cmd.getOptionValue("i"); System.out.println("Input file: " + inputFile); } catch (ParseException e) { System.out.println("Error parsing command line parameters: " + e.getMessage()); } } } <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.4</version> </dependency>


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