Maven: <dependency> <groupId>net.sourceforge.argparse4j</groupId> <artifactId>argparse4j</artifactId> <version>0.9.0</version> </dependency> Gradle: groovy dependencies { implementation 'net.sourceforge.argparse4j:argparse4j:0.9.0' } import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.inf.ArgumentParser; import net.sourceforge.argparse4j.inf.ArgumentParserException; import net.sourceforge.argparse4j.inf.Namespace; public class CommandLineParser { public static void main(String[] args) { ArgumentParser parser = ArgumentParsers.newFor("CommandLineParser").build() .description("This is a command line parser example."); parser.addArgument("-f", "--file") .required(true) .help("Path to the input file."); parser.addArgument("-v", "--verbose") .action(Arguments.storeTrue()) .help("Enable verbose mode."); parser.addArgument("-n", "--number") .type(Integer.class) .setDefault(1) .help("Number of iterations."); try { Namespace namespace = parser.parseArgs(args); String inputFile = namespace.getString("file"); boolean isVerbose = namespace.getBoolean("verbose"); int iterations = namespace.getInt("number"); System.out.println("Input file: " + inputFile); System.out.println("Verbose mode: " + isVerbose); System.out.println("Iterations: " + iterations); } catch (ArgumentParserException e) { parser.handleError(e); System.exit(1); } } } bash java CommandLineParser -f input.txt -v -n 5 Input file: input.txt Verbose mode: true Iterations: 5


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