Add define-to-enum.rb script
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 20 Oct 2010 20:35:32 +0000 (13:35 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 20 Oct 2010 20:35:32 +0000 (13:35 -0700)
define-to-enum.rb [new file with mode: 0755]

diff --git a/define-to-enum.rb b/define-to-enum.rb
new file mode 100755 (executable)
index 0000000..6e1e0df
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/ruby -w
+
+require 'optparse'
+require 'ostruct'
+require 'pp'
+
+##################### CLASSES ##################### 
+class MyOptionParse
+  def self.parse(args)
+    # default values 
+    opts = OpenStruct.new
+    opts.width = 70
+
+    parser = OptionParser.new do |myparser|
+      myparser.banner = "Usage: #{$0} [opts]"
+      myparser.separator "Specific options:"
+      myparser.on("-w WIDTH", "--width",
+              "The maximum width of the enum identifier.") do |a|
+        opts.width = a.to_i
+      end
+    end
+
+    parser.parse!(args)
+    return opts
+  end
+end
+
+##################### FUNCTIONS ##################### 
+##################### CODE ##################### 
+$opts = MyOptionParse.parse(ARGV)
+#pp opts
+
+STDIN.each_line do |line|
+  line.chomp!
+  if line =~ /\#define[ \t]([^ \t]*)[ \t][ \t]*([^ \t]*)/ then
+    ident  = $1
+    val = $2
+    ident2 = "#{ident} = "
+    printf("  %-#{$opts.width}s %s,\n", ident2, val)
+  else
+    puts line
+  end
+end