#!/usr/bin/env ruby # # Prints out the input where all the camel-case is converted into # '_'-separated words # require 'open-uri' class Camelcase2Underscores def initialize @camelcase2underscores = {} end # Main entry. Exits with exit code from main_with_exit_code. def main(argv) exit main_with_exit_code argv end private # Converts the files, urls, or strings in argv and prints the # results to STDOUT. Returns an exit code on completion. def main_with_exit_code(argv) # Options argv.find_all {|x| x =~ /^-/}.each do |arg| @opt_v = true if arg =~ /-v/ @opt_p = true if arg =~ /-p/ @opt_h = true if arg =~ /-h/ @opt_l = true if arg =~ /-l/ @opt_r = true if arg =~ /-r/ end # Not option flags possible_inputs = argv.find_all {|x| x !~ /^-/} # Error for no inputs if possible_inputs.empty? error 'No input files/urls/strings given' print_help return 1 end # Print help? not an error if @opt_h print_help return 0 end # Prints out the error and returns a 1 def handle_error(e,type,input) error 'Problem reading from ' + type + ' ' + q(input) error e 1 end # Scans the array, arr, yielding every line def scan(arr,type,input) exit_code = 0 note 'Converting ' + type + ' ' + q(input) begin arr.each do |line| convert_and_print line end rescue Exception => e exit_code += handle_error e,'File',input end exit_code end # Convert the inputs exit_code = 0 possible_inputs.each do |input| if File.exist? input exit_code += scan IO.readlines(input),'file',input elsif input =~ /\w+tp:\/\// exit_code += scan open(input).read.split(/\n/),'URL',input else exit_code += scan [input],'string',input end end # Print the report? print_report if @opt_r # OK exit_code end # Prints to STDERR def e(s='') STDERR.puts s end # Print a report of the conversion made def print_report e '==================== Report ====================' e @camelcase2underscores.length.to_s + ' conversions:' e longest_word = '' @camelcase2underscores.each do |from,to| longest_word = from if from.length > longest_word.length end @camelcase2underscores.each do |from,to| e from + (' ' * (longest_word.length+1-from.length)) + ' -> ' + to end end # Prints the error message, s def error(s) e s end # Print the options def print_help e 'Usage ' + File.basename($0) + ' ' e 'where options include:' e ' -h | -help print this message' e ' -v | -verbose verbosely print what we\'re doing' e ' -l | -loud *very* verbosely print what we\'re doing' e ' -p | -print echo the input' e ' -r | -report print report at the end' e 'and inputs are files, URLs, or strings.' end # Quotes the string, s def q(s) "'" + s + "'" end # Prints a message if we're in verbose mode--i.e. passed a -v def note(msg) STDERR.puts msg if @opt_v end # Prints a message if we're in loud mode--i.e. passed a -l def note_loud(msg) STDERR.puts msg if @opt_l end # Convert the line and print it to STDOUT def convert_and_print(line) STDERR.puts line if @opt_p note_loud '> ' + line $line = line line.scan /([a-z]+)([A-Z]+)([a-z0-9]*)/ do |res| prefix,ups,rest = res word = prefix + ups + rest new_word = prefix + '_' + ups.downcase new_word += '_' if rest.length>0 && ups.length>1 new_word += rest note_loud word + ' -> ' + new_word @camelcase2underscores[word] = new_word $line = $line.gsub word,new_word end note_loud '< ' + $line puts $line end end Camelcase2Underscores.new.main ARGV