Commit in SlicDiagnostics/src/org/lcsim/slic/diagnostics on MAIN
CommandLineProcessor.java+194-291.3 -> 1.4
SlicDiagnosticsDriver.java+2-21.16 -> 1.17
+196-31
2 modified files
JM: Add command line options for directories to search for input files and pattern matching

SlicDiagnostics/src/org/lcsim/slic/diagnostics
CommandLineProcessor.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- CommandLineProcessor.java	23 Feb 2006 00:37:46 -0000	1.3
+++ CommandLineProcessor.java	1 Mar 2006 00:32:19 -0000	1.4
@@ -16,13 +16,18 @@
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
 
+import org.apache.commons.io.filefilter.SuffixFileFilter;
+import org.apache.commons.io.filefilter.IOFileFilter;
+import org.apache.commons.io.filefilter.FileFilterUtils;
+import org.apache.commons.io.filefilter.WildcardFilter;
+
 /**
  * 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.3 2006/02/23 00:37:46 jeremy Exp $
+ * @version $Id: CommandLineProcessor.java,v 1.4 2006/03/01 00:32:19 jeremy Exp $
  */
 public class CommandLineProcessor
 {
@@ -32,15 +37,18 @@
     private List<File> _infiles = new ArrayList<File>();
     private File _outfile = null;
     private int _maxevents = -1;
-    private int _verbosity = 1;
+    private int _verbosity = 0;
 
-    /** Create a CommandLineProcessor where parse() will be called later. */
+    /** Create a CommandLineProcessor where parse will be called later. */
     public CommandLineProcessor()
     {
         defineOptions();
     }
 
-    /** Create a CommandLineProcessor and call parse on input args. */
+    /**
+     * Create a CommandLineProcessor and call parse on the supplied command line
+     * arguments.
+     */
     public CommandLineProcessor(String args[])
     {
         defineOptions();
@@ -58,87 +66,208 @@
     /** Define the command-line options for SlicDiagnostics. */
     private void defineOptions()
     {
-        Option opt = new Option("f", false, "file containing list of files to process");
+        Option opt = new Option("f", false, "add a file containing a list of LCIO files to process (one per line)");
         opt.setArgs(1);
         _options.addOption(opt);
 
-        opt = new Option("i", false, "input files or patterns");
+        opt = new Option("i", false, "add a single input file with no pattern matching");
         opt.setArgs(1);
         _options.addOption(opt);
 
-        opt = new Option("n", false, "max events to process");
+        opt = new Option("n", false, "set the maximum number of events to process");
         opt.setArgs(1);
         _options.addOption(opt);
 
-        opt = new Option("o", false, "output file name");
+        opt = new Option("o", false, "set the 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 = new Option("v", false, "set the verbosity");
+        opt.setArgs(1);
+        _options.addOption(opt);
+
+        opt = new Option("d", false, "add a search directory for file patterns (no pattern uses *.slcio)");
+        opt.setArgs(1);
+        _options.addOption(opt);
+
+        opt = new Option("p", false, "add a pattern to match files in directories (no directories uses current dir)");
         opt.setArgs(1);
         _options.addOption(opt);
     }
 
-    /** Parse command-line options. */
+    /** Parse the command-line options. */
     public void parse(String args[]) throws ParseException
     {
+        // Parse the args using commons-cli
         CommandLine cl = _parser.parse(_options, args);
 
-        if (cl.hasOption("h") || args.length == 0)
+        // Print usage if no args or -h option was specified
+        if (cl.hasOption("h") || args.length == 0 || 
+                (!cl.hasOption("f") && !cl.hasOption("i") && !cl.hasOption("d") && !cl.hasOption("p")))
         {
-           printUsage(true);
+            printUsage(true);
         }
-
+        
+        // Process lists of files
+        String infileList[] = null;
         if (cl.hasOption("f") || args.length == 1)
         {
-            String infileList = null;
             if (cl.hasOption("f"))
             {
-                infileList = cl.getOptionValue("f");
+                infileList = cl.getOptionValues("f");
             }
             else if (args.length == 1)
             {
-                infileList = args[0];
+                infileList = new String[1];
+                infileList[0] = args[0];
             }
-            readInputFileList(infileList);
+            readInputFileLists(infileList);
         }
 
+        // Process verbatim input files
+        String verbatimInputFiles[] = null;
         if (cl.hasOption("i"))
         {
-            String vals[] = cl.getOptionValues("i");
-            for (String f : vals)
+            verbatimInputFiles = cl.getOptionValues("i");
+            for (String f : verbatimInputFiles)
             {
                 _infiles.add(new File(f));
             }
         }
 
+        // Max events
         if (cl.hasOption("n"))
-        {            
+        {
             _maxevents = Integer.parseInt(cl.getOptionValue("n"));
         }
 
+        // Verbosity
         if (cl.hasOption("v"))
         {
             _verbosity = Integer.parseInt(cl.getOptionValue("v"));
         }
 
+        // Directories
+        List<File> userDirs = new ArrayList<File>();
+        if (cl.hasOption("d"))
+        {
+            String dirs[] = cl.getOptionValues("d");
+            for (String dir : dirs)
+            {
+                userDirs.add(new File(dir));
+            }
+        }
+
+        // Process input file patterns
+        List<String> patternList = new ArrayList<String>();
+        if (cl.hasOption("p"))
+        {
+            // Search curr dir if none specified
+            if (userDirs.size() == 0)
+            {
+                userDirs.add(new File("."));
+            }
+
+            String patterns[] = cl.getOptionValues("p");
+
+            for (String pattern : patterns)
+            {
+                patternList.add(pattern);
+            }
+        }
+        else
+        {
+            if (cl.hasOption("d"))
+            {
+                if (patternList.size() == 0)
+                {
+                    patternList.add("*.slcio");
+                }
+            }
+        }
+
+        // Add input files from directory and pattern arguments
+        List<File> matches = new ArrayList<File>();
+
+        if (patternList.size() > 0 && userDirs.size() > 0)
+        {
+            matches = matchFiles(userDirs, patternList);
+
+            if (matches.size() != 0)
+            {
+                for (File match : matches)
+                {
+                    _infiles.add(match);
+                }
+            }
+            else
+            {
+                System.err.println("WARNING: No matches found from patterns.");
+            }
+        }
+
+        // If running in verbose mode, print out the results from command line
+        // arguments that have been processed, thusfar.
+        if (_verbosity > 0)
+        {
+            if (infileList != null)
+            {
+                for (String f : infileList)
+                {
+                    System.out.println("filelist=" + infileList);
+                }
+            }
+
+            if (userDirs.size() > 0)
+            {
+                for (File file : userDirs)
+                {
+                    System.out.println("directory=" + file.getAbsoluteFile());
+                }
+            }
+
+            if (patternList.size() > 0)
+            {
+                for (String s : patternList)
+                {
+                    System.out.println("pattern=" + s);
+                }
+            }
+
+            for (File file : _infiles)
+            {
+                System.out.println("inputFile=" + file.getAbsoluteFile());
+            }
+        }
+
+        // There should be some files to process at this point, or the command
+        // line
+        // arguments did not find any input files.
         if (_infiles.size() == 0)
         {
+            System.err.println("No input files were found from command line arguments.");
             printUsage(true);
         }
 
+        // Name the output file
         if (cl.hasOption("o"))
         {
             _outfile = new File(cl.getOptionValue("o"));
         }
+        // Or autoname the output file
         else
         {
             String outfile = _infiles.get(0).toString().replace(".slcio", "");
             _outfile = new File(outfile);
         }
+
+        if (_verbosity > 0)
+        {
+            System.out.println("outputFile=" + _outfile.getAbsoluteFile());
+        }
     }
 
     /** Get the list of input files created by parse(). */
@@ -169,7 +298,7 @@
     private void printUsage(boolean exit)
     {
         HelpFormatter help = new HelpFormatter();
-        help.printHelp("SlicDiagnostics", _options);
+        help.printHelp("SlicDiagnostics", "At least one of -d, -f, -i, or -p is required.", _options, "");
         if (exit)
         {
             System.exit(0);
@@ -180,22 +309,58 @@
      * Convert a line-delimited list of input files into a List of java File
      * objects.
      */
-    private void readInputFileList(String filelist)
+    private void readInputFileLists(String filelist[])
     {
-        try
+        for (String f : filelist)
         {
-            BufferedReader lines = getBufferedReader(filelist);
-            String line;
+            try
+            {
+                BufferedReader lines = getBufferedReader(f);
+                String line;
 
-            while ((line = lines.readLine()) != null)
+                while ((line = lines.readLine()) != null)
+                {
+                    _infiles.add(new File(line.trim()));
+                }
+            }
+            catch (Exception e)
             {
-                _infiles.add(new File(line.trim()));
+                throw new RuntimeException("Problem with input file list.", e);
             }
         }
-        catch (Exception e)
+    }
+
+    /**
+     * Find files in a list of directories that match one of the patterns in a
+     * list.
+     */
+    private List<File> matchFiles(List<File> dirs, List<String> patterns)
+    {
+        SuffixFileFilter lcioFilter = new SuffixFileFilter(".slcio");
+
+        List<File> matches = new ArrayList<File>();
+
+        for (File dir : dirs)
         {
-            throw new RuntimeException("Problem running from file.", e);
+            if (!dir.isDirectory())
+            {
+                throw new RuntimeException(dir.getAbsolutePath() + " is not a directory.");
+            }
+
+            for (String pattern : patterns)
+            {
+                IOFileFilter filter = FileFilterUtils.andFileFilter(lcioFilter, new WildcardFilter(pattern));
+
+                String[] files = dir.list(filter);
+
+                for (String file : files)
+                {
+                    matches.add(new File(dir + File.separator + file));
+                }
+            }
         }
+
+        return matches;
     }
 
     /** Simple utility function to get a BufferedReader. */

SlicDiagnostics/src/org/lcsim/slic/diagnostics
SlicDiagnosticsDriver.java 1.16 -> 1.17
diff -u -r1.16 -r1.17
--- SlicDiagnosticsDriver.java	17 Feb 2006 01:26:34 -0000	1.16
+++ SlicDiagnosticsDriver.java	1 Mar 2006 00:32:19 -0000	1.17
@@ -18,7 +18,7 @@
  * A mother driver that loads all of the package's subdrivers.
  *
  * @author jeremym
- * @version $Id: SlicDiagnosticsDriver.java,v 1.16 2006/02/17 01:26:34 jeremy Exp $
+ * @version $Id: SlicDiagnosticsDriver.java,v 1.17 2006/03/01 00:32:19 jeremy Exp $
  */
 public class SlicDiagnosticsDriver extends Driver
 {
@@ -28,7 +28,7 @@
     private String _fileName = null;
     private static final String _defaultFileName = "SlicDiagnostics";            
     private EventHeader _currentEvent = null;
-    private int _verbosity = 1;
+    private int _verbosity = 0;
     
     private int _evtCnt = 0;
     
CVSspam 0.2.8