Home > Java > javaTutorial > How to Effectively Parse Command Line Arguments in Java?

How to Effectively Parse Command Line Arguments in Java?

Mary-Kate Olsen
Release: 2024-12-23 01:14:10
Original
363 people have browsed it

How to Effectively Parse Command Line Arguments in Java?

Parse Command Line Arguments in Java

Parsing command line arguments is a common task when working with Java applications. Various approaches exist to accomplish this effectively.

Libraries for Parsing

  • Apache Commons CLI: This library provides a comprehensive set of options for parsing command line arguments, including support for required and optional flags, argument validation, and error handling.
  • JSAP: The Java Simple Argument Parser (JSAP) offers a simpler and more lightweight option for parsing arguments. It supports both named and positional arguments, as well as option grouping.

Rolling Your Own

If you prefer to avoid external libraries, you can roll your own command line argument parser using Java's built-in classes. The java.util.Scanner class allows you to read input from the command line and parse it into the appropriate data types.

Example: Parsing Strings with Commons CLI

import org.apache.commons.cli.*;

public class Main {

    public static void main(String[] args) throws Exception {

        // Define command line options
        Options options = new Options();
        Option input = new Option("i", "input", true, "input file path");
        input.setRequired(true);
        options.addOption(input);
        Option output = new Option("o", "output", true, "output file");
        output.setRequired(true);
        options.addOption(output);

        // Parse command line arguments
        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = null; // Bad practice, only for demonstration purposes
        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("utility-name", options);
            System.exit(1);
        }

        // Retrieve parsed values
        String inputFilePath = cmd.getOptionValue("input");
        String outputFilePath = cmd.getOptionValue("output");

        // Use the parsed arguments
        System.out.println(inputFilePath);
        System.out.println(outputFilePath);
    }
}
Copy after login

Usage:

$ java -jar target/my-utility.jar -i asd

Missing required option: o

usage: utility-name
 -i,--input <arg>   input file path
 -o,--output <arg>  output file
Copy after login

The above is the detailed content of How to Effectively Parse Command Line Arguments in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template