Print

Print


Commit in SlicDiagnostics/src/org/lcsim/slic/diagnostics on MAIN
CommandLineProcessor.java+81-651.1 -> 1.2
JM: Add usage method

SlicDiagnostics/src/org/lcsim/slic/diagnostics
CommandLineProcessor.java 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- CommandLineProcessor.java	17 Feb 2006 01:26:34 -0000	1.1
+++ CommandLineProcessor.java	17 Feb 2006 22:24:25 -0000	1.2
@@ -17,34 +17,36 @@
 import org.apache.commons.cli.PosixParser;
 
 /**
- * CommandLineProcessor parses command line options and arguments specific to SlicDiagnostics.  
- * The resulting parameters are passed to the Runner class in Main.
+ * CommandLineProcessor parses command line options and arguments specific to
+ * SlicDiagnostics. The resulting parameters are passed to the Runner class in
+ * Main.
  * 
  * @author Jeremy McCormick
- * @version $Id: CommandLineProcessor.java,v 1.1 2006/02/17 01:26:34 jeremy Exp $
+ * @version $Id: CommandLineProcessor.java,v 1.2 2006/02/17 22:24:25 jeremy Exp $
  */
 public class CommandLineProcessor
 {
     private Options _options = new Options();
     private CommandLineParser _parser = new PosixParser();
-        
+
     private List<File> _infiles = new ArrayList<File>();
     private File _outfile = null;
     private int _maxevents = -1;
     private int _verbosity = 1;
-    
+
     /** Create a CommandLineProcessor where parse() will be called later. */
     public CommandLineProcessor()
-    {               
+    {
         defineOptions();
-    }    
-    
+    }
+
     /** Create a CommandLineProcessor and call parse on input args. */
     public CommandLineProcessor(String args[])
-    {               
+    {
         defineOptions();
-        
-        try {
+
+        try
+        {
             parse(args);
         }
         catch (ParseException pe)
@@ -52,45 +54,44 @@
             throw new RuntimeException("Problem parsing args", pe);
         }
     }
-    
+
     /** Define the command-line options for SlicDiagnostics. */
     private void defineOptions()
     {
-        Option opt = new Option("f",false,"file containing list of files to process"); 
-        opt.setArgs(1);                  
-        _options.addOption(opt); 
-        
-        opt = new Option("i",false,"input files or patterns"); 
-        _options.addOption(opt);
-        
-        opt = new Option("n",false,"max events to process");       
-        _options.addOption(opt); 
-        
-        opt = new Option("o",false,"output file name"); 
-        opt.setArgs(1);        
-        _options.addOption(opt); 
-        
-        opt = new Option("h", false,"print usage");               
-        _options.addOption(opt); 
-        
-        opt = new Option("v",false,"set verbosity"); opt.setArgs(1);
-        _options.addOption(opt);        
+        Option opt = new Option("f", false, "file containing list of files to process");
+        opt.setArgs(1);
+        _options.addOption(opt);
+
+        opt = new Option("i", false, "input files or patterns");
+        _options.addOption(opt);
+
+        opt = new Option("n", false, "max events to process");
+        _options.addOption(opt);
+
+        opt = new Option("o", false, "output file name");
+        opt.setArgs(1);
+        _options.addOption(opt);
+
+        opt = new Option("h", false, "print usage");
+        _options.addOption(opt);
+
+        opt = new Option("v", false, "set verbosity");
+        opt.setArgs(1);
+        _options.addOption(opt);
     }
-    
+
     /** Parse command-line options. */
     public void parse(String args[]) throws ParseException
     {
         CommandLine cl = _parser.parse(_options, args);
-               
+
         if (cl.hasOption("h") || args.length == 0)
         {
-            HelpFormatter help = new HelpFormatter();
-            help.printHelp("SlicDiagnostics", _options);
-            System.exit(0);
+           printUsage(true);
         }
-        
+
         if (cl.hasOption("f") || args.length == 1)
-        {     
+        {
             String infileList = null;
             if (cl.hasOption("f"))
             {
@@ -101,75 +102,90 @@
                 infileList = args[0];
             }
             readInputFileList(infileList);
-        }            
-        
+        }
+
         if (cl.hasOption("i"))
-        {            
-            String vals[] = cl.getOptionValues("i");            
+        {
+            String vals[] = cl.getOptionValues("i");
             for (String f : vals)
-            {               
+            {
                 _infiles.add(new File(f));
             }
         }
-                
+
         if (cl.hasOption("n"))
         {
             _maxevents = Integer.parseInt(cl.getOptionValue("n"));
         }
-        
+
         if (cl.hasOption("v"))
         {
             _verbosity = Integer.parseInt(cl.getOptionValue("v"));
-        }        
-        
+        }
+
         if (_infiles.size() == 0)
         {
-            throw new RuntimeException("Didn't find any input files to process.");
+            printUsage(true);
         }
-        
+
         if (cl.hasOption("o"))
         {
             _outfile = new File(cl.getOptionValue("o"));
-        }        
-        else {
-            String outfile = _infiles.get(0).toString().replace(".slcio","");
+        }
+        else
+        {
+            String outfile = _infiles.get(0).toString().replace(".slcio", "");
             _outfile = new File(outfile);
-        }       
+        }
     }
-    
+
     /** Get the list of input files created by parse(). */
     public List<File> getInputFileList()
     {
         return _infiles;
     }
-    
+
     /** Get the output file name. */
     public File getOutputFile()
     {
         return _outfile;
     }
-    
+
     /** Get the verbosity. */
     public int verbosity()
     {
         return _verbosity;
     }
-    
+
     /** Get the maximum number of events to process. */
     public int maxEvents()
     {
         return _maxevents;
     }
-    
-    /** Convert a line-delimited list of input files into a List of java File objects. */
+
+    /** Print usage and exit (optionally). */
+    private void printUsage(boolean exit)
+    {
+        HelpFormatter help = new HelpFormatter();
+        help.printHelp("SlicDiagnostics", _options);
+        if (exit)
+        {
+            System.exit(0);
+        }
+    }
+
+    /**
+     * Convert a line-delimited list of input files into a List of java File
+     * objects.
+     */
     private void readInputFileList(String filelist)
     {
         try
         {
             BufferedReader lines = getBufferedReader(filelist);
             String line;
-            
-            while ( (line = lines.readLine()) != null)
+
+            while ((line = lines.readLine()) != null)
             {
                 _infiles.add(new File(line.trim()));
             }
@@ -177,13 +193,13 @@
         catch (Exception e)
         {
             throw new RuntimeException("Problem running from file.", e);
-        }            
+        }
     }
-           
+
     /** Simple utility function to get a BufferedReader. */
     static private BufferedReader getBufferedReader(String fileName) throws IOException
     {
-        FileInputStream fin =  new FileInputStream(fileName);
+        FileInputStream fin = new FileInputStream(fileName);
         return new BufferedReader(new InputStreamReader(fin));
-    }                      
+    }
 }
CVSspam 0.2.8