From: Colin Patrick McCabe Date: Wed, 20 Oct 2010 20:35:32 +0000 (-0700) Subject: Add define-to-enum.rb script X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a7298b122f57742589e973a971543eb925618eb;p=cmccabe-bin Add define-to-enum.rb script --- diff --git a/define-to-enum.rb b/define-to-enum.rb new file mode 100755 index 0000000..6e1e0df --- /dev/null +++ b/define-to-enum.rb @@ -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