How to use Apache Commons CLI to parse the property file and --help option?

2.6k views Asked by At

I have a property file which is like this -

hostName=machineA.domain.host.com
[email protected]
[email protected]
[email protected]

And now I am reading the above property file from my Java program as shown below. I am parsing the above property file manual way as of now -

public class FileReaderTask {
    private static String hostName;
    private static String emailFrom;
    private static String emailTo;
    private static String emailCc;

    private static final String configFileName = "config.properties";
    private static final Properties prop = new Properties();

    public static void main(String[] args) {
        readConfig(arguments);

        // use the above variables here
        System.out.println(hostName);
        System.out.println(emailFrom);
        System.out.println(emailTo);
        System.out.println(emailCc);
    }

    private static void readConfig(String[] args) throws FileNotFoundException, IOException {
        if (!TestUtils.isEmpty(args) && args.length != 0) {
            prop.load(new FileInputStream(args[0]));
        } else {
            prop.load(FileReaderTask.class.getClassLoader().getResourceAsStream(configFileName));
        }

        StringBuilder sb = new StringBuilder();
        for (String arg : args) {
            sb.append(arg).append("\n");
        }
        String commandlineProperties = sb.toString();

        if (!commandlineProperties.isEmpty()) {
            // read, and overwrite, properties from the commandline...
            prop.load(new StringReader(commandlineProperties));
        }           

        hostName = prop.getProperty("hostName").trim();
        emailFrom = prop.getProperty("emailFrom").trim();
        emailTo = prop.getProperty("emailTo").trim();
        emailCc = prop.getProperty("emailCc").trim();
    }
}

Most of the time, I will be running my above program through command line as a runnable jar like this -

java -jar abc.jar config.properties
java -jar abc.jar config.properties hostName=machineB.domain.host.com

My question is-

  • Is there any way to add --help option while running the abc.jar that can tell us more about how to run the jar file and what does each property means and how to use them? I have seen --help while running most of the C++ executable or Unix stuff so not sure how we can do the same thing in Java?

Do I need to use CommandLine parser like Commons CLI for this in Java to achieve this and instead of doing manual parsing, I should use Commons CLI to parse the file as well? If yes, then can anyone provide an example how would I do that in my scenario?

1

There are 1 answers

4
centic On

In the long run if you plan to add other options in the future then commons-cli is surely a fairly good fit as it makes it easy to add new options and manual parsing quickly becomes complicated.

Take a look at the official examples, they provide a good overview of what the library can do.

Your specific case would probably lead to something like the following:

    // create Options object
    Options options = new Options();

    Option help = new Option( "h", "help", false, "print this message" );
    options.addOption(help);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse( options, args);

    if(cmd.hasOption("help") || cmd.getArgList().isEmpty()) {
        // automatically generate the help statement
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp( "cli-test [options] <property-file>", options );

        return;
    }

    // do your thing...
    System.out.println("Had properties " + cmd.getArgList());