OSDN Git Service

Fix #36855: Allow util/statistics.rb to set a filtering condition of found files
authorDaigo Moriwaki <beatles@users.sourceforge.jp>
Fri, 9 Dec 2016 12:15:41 +0000 (21:15 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Fri, 9 Dec 2016 12:15:41 +0000 (21:15 +0900)
Regarding util/statistics.rb, add a new command line option,
--filter regexp, to process files that are matched with a regexp.
It would make it easier to calculate statistics of certain games.

changelog
utils/statistics.rb

index 484910f..3065ac2 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,11 @@
+2016-12-09  Daigo Moriwaki <daigo at debian dot org>
+
+       * Allow util/statistics.rb to set a filtering condition of found files
+         Regarding util/statistics.rb, add a new command line option,
+         --filter regexp, to process files that are matched with a regexp.
+         It would make it easier to calculate statistics of certain games.
+         (Closes #36855)
+
 2016-11-26  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server] Allow to customize maximum lenght of a login indentifier
index c98ee45..a722049 100755 (executable)
@@ -139,15 +139,30 @@ end
 
 if $0 == __FILE__
   def usage
-    puts "Usage: #{$0} [OPTIONS] dir [...]"
-    puts "Options:"
+puts <<-EOF
+SYNOPSIS:
+    #{$0} [OPTION]... file or dir [...]
+
+DESCRIPTION:
+    It calculates game statistics, reading CSA record files.
+    If an argument is a directory, it attempts to find files in its
+    subdirectories recursively.
+
+    --filter regexp
+        only files that are matched with a regexp are processed (default: no filter)
+
+    --repeat n
+        at most n files are processed (default: unlimitted)
+EOF
     exit 1
   end
 
   usage if ARGV.empty?
 
   parser = GetoptLong.new(
-             ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT]
+             ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT],
+             ['--filter', GetoptLong::REQUIRED_ARGUMENT],
+             ['--h', '-h', GetoptLong::NO_ARGUMENT]
            )
   begin
     parser.each_option do |name, arg|
@@ -157,6 +172,10 @@ if $0 == __FILE__
     usage
   end
 
+  if $OPT_H
+    usage
+  end
+
   $OPT_REPEAT = $OPT_REPEAT.to_i
   if $OPT_REPEAT == 0
     $OPT_REPEAT = -1
@@ -167,11 +186,15 @@ if $0 == __FILE__
     if FileTest.directory?(cmd)
       Dir.glob(File.join(cmd, "**", "*.csa")).each do |file|
         break if $OPT_REPEAT == 0
-        do_file(file)
+        if $OPT_FILTER.nil? || /#{$OPT_FILTER}/ =~ file
+          do_file(file)
+        end
       end
     elsif FileTest.file?(cmd)
       break if $OPT_REPEAT == 0
-      do_file(cmd)
+      if $OPT_FILTER.nil? || /#{$OPT_FILTER}/ =~ cmd
+        do_file(cmd)
+      end
     else
       throw "Unknown file or directory: #{cmd}"
     end