Skip to main content

Dealing with Command Line Parameters

Posted by tchangu on December 19, 2005 at 9:43 AM PST

One of the things that keep popping up often is a need to write batch programs that is started from command line. Every single time its always been either reinvent the wheel or copy/paste from old code. So recently decided to put an end to this practice by looking at CLI solution from Apache.


CLI is a really nice utility and could be a potential enterprise reusable component. However while dealing with CLI, one will soon discover the verbosity especially while dealing with OptionBuilder
class. This means we are reverting back to old manner of programming (cut/paste) but in style.

But one could externalize this in a XML by hiding the plumbing of CLI that deals with verbosity. A simple framework around CLI framework would do this trick and this means a developer who wants to use CLI would write this simple XML


<cli-map>
   <cli-parser>org.apache.commons.cli.GnuParser</cli-parser>
   <cli-domain-obj>com.util.cli.domain.CommandObj</cli-domain-obj>
   <help>
    <help-formatter>org.apache.commons.cli.HelpFormatter</help-formatter>
      <print-help>true</print-help>
    <syntax-tip>java com.util.cli.TestDrive blah blah </syntax-tip>
   </help>
   <options>
            <option optName="file"  withArgName="file" 
        hasArgs="true" withDescription="use given file for log" />
            <option optName="logger" withArgName="classname"
        hasArgs="true" withDescription="the class which it to perform logging" />
            <option optName="listener" withArgName="classname"
        hasArgs="true" withDescription="add an instance of class as a project listener" />
            <option optName="buildfile" withArgName="file"
        hasArgs="true" withDescription ="use given buildfile" />
            <option optName="file" withArgName="file"
        hasArgs="true" withDescription="search for buildfile towards the root of the filesystem and use it" />
            <option optName="D" withArgName="property=value" 
    hasArgs="true" withValueSeparator="=" withDescription ="use value for given property" />
    </options>
</cli-map>


The com.util.cli.domain.CommandObj needs to have instance variables that is same as optName. Then, using reflection and XML processing one could do bulk of the CLI plumbing to figure out what the values are. The outcome of this simple exercise - No more need to go through drudgery of writing code to parse command line arguments.
Write a simple XML, create a POJO, and get values populated in the POJO that is needed for the batch program with just three lines of code.


String fileName = "cli-map.xml";
CommandProcessorExt cmdProcessor = new CommandProcessorExt(fileName);
CommandObj obj = (CommandObj)cmdProcessor.process(args);

Better yet, Commons Configuration could be used to implement something similar! too...
Of course if some one is interested in the rudimentary, but working, source code, please feel free to ping!

Related Topics >>