Search code examples
javaapache-commons-cli

how to use CommandLine and Options in apache commons-cli


This is first time am using apache commons-cli.

Requirement : i want to use CommandLine and Options in commons-cli to pass my runtime parameters to my java class.

Scenario : main class - com.test.mian.MyClass

i can run my class from command line by

java -cp $classPath  com.test.mian.MyClass -one 1 -two 2 -three 3

how can i do the same from a method of another class by passing these arguments using CommandLine and Options of commons-cli.

and also if there is any other way other than

System.setProperty("key","value");

please suggest too.


Solution

  • Here's a very stripped down version of something I use. It basically sets up a singleton that gets initialized once and then can be used wherever you want within your program. I chose to store the info in HashMap's and ArrayList's because they were easier to deal with later.

    //****************************************************************************
    //***** File Name: MianCLIOptions.java
    //****************************************************************************
    
    package com.test.mian;
    
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.apache.commons.cli.AlreadySelectedException;
    import org.apache.commons.cli.CommandLine;
    import org.apache.commons.cli.CommandLineParser;
    import org.apache.commons.cli.GnuParser;
    import org.apache.commons.cli.MissingArgumentException;
    import org.apache.commons.cli.MissingOptionException;
    import org.apache.commons.cli.Option;
    import org.apache.commons.cli.Options;
    import org.apache.commons.cli.ParseException;
    import org.apache.commons.cli.UnrecognizedOptionException;
    import org.apache.commons.lang.WordUtils;
    
    
    //****************************************************************************
    //****************************************************************************
    //****************************************************************************
    //****************************************************************************
    public class MianCLIOptions
    {
        //***** constants *****
    
        //***** public data members *****
    
        //***** private data members *****
        private static MianCLIOptions singletonObj = null;
    
        private HashMap<String,Object> options = new HashMap<String,Object>();
        private ArrayList<String> arguments = new ArrayList<String>();
    
        //****************************************************************************
        public static MianCLIOptions getopts()
        {
            if (singletonObj == null) {
                throw new IllegalStateException("[MianCLIOptions] Command line not yet initialized.");
            }
    
            return singletonObj;
        }
    
        //****************************************************************************
        public static synchronized void initialize(Options optsdef, String[] args)
            throws MianCLIOptionsException, UnrecognizedOptionException, MissingArgumentException,
                   MissingOptionException, AlreadySelectedException, ParseException
        {
            if (singletonObj == null) {
                singletonObj = new MianCLIOptions(optsdef, args);
            }
            else {
                throw new IllegalStateException("[MianCLIOptions] Command line already initialized.");
            }
        }
    
        //****************************************************************************
        //----- prevent cloning -----
        public Object clone() throws CloneNotSupportedException
        {
            throw new CloneNotSupportedException();
        }
    
        //****************************************************************************
        public boolean isset(String opt)
        {
            return options.containsKey(opt);
        }
    
        //****************************************************************************
        public Object getopt(String opt)
        {
            Object rc = null;
    
            if (options.containsKey(opt)) {
                rc = options.get(opt);
            }
    
            return rc;
        }
    
        //****************************************************************************
        //***** finally parse the command line
        //****************************************************************************
        private MianCLIOptions(Options optsdef, String[] args)
            throws UnrecognizedOptionException, MissingArgumentException,
                   MissingOptionException, AlreadySelectedException, ParseException
        {
            //***** (blindly) parse the command line *****
            CommandLineParser parser = new GnuParser();
            CommandLine cmdline = parser.parse(optsdef, args);
    
            //***** store options and arguments *****
            //----- options -----
            for (Option opt : cmdline.getOptions()) {
                String key = opt.getOpt();
                if (opt.hasArgs()) {
                    options.put(key, opt.getValuesList());
                }
                else {
                    options.put(key, opt.getValue());
                }
            }
    
            //----- arguments -----
            for (String str : cmdline.getArgs()) {
                //----- account for ant/build.xml/generic -----
                if (str.length() > 0) {
                    arguments.add(str);
                }
            }
        }
    }
    
    //****************************************************************************
    //***** EOF  ***** EOF  ***** EOF  ***** EOF  ***** EOF  ***** EOF  **********
    

    In your main, you can then call it like so:

        //***** build up options *****
        Options options = new Options();
    
        // ... .... ...
    
        //***** process command line *****
        try {
            MianCLIOptions.initialize(options, args);
        }
        catch (UnrecognizedOptionException ex) {
            // do something
        }
    

    And finally, in some other class you can call it like so:

        MianCLIOptions opts = MianCLIOptions.getopts();
        if (opts.isset("someopt")) {
            // do something exciting
        }
    

    Hope that helps!