lib/findr/cli.rb in findr-0.0.2 vs lib/findr/cli.rb in findr-0.0.3

- old
+ new

@@ -1,29 +1,35 @@ #!/usr/bin/env ruby require 'pathname' require 'tempfile' require 'optparse' +require 'yaml' # this is deprecated on 1.9, but still works fine require 'iconv' +require 'findr/version' + + module Findr class CLI + CONFIGFILE = '.findr-config' + def colorize(text, color_code) "\e[#{color_code}m#{text}\e[0m" end def red(text); colorize(text, 31); end def green(text); colorize(text, 32); end def yellow(text); colorize(text, 33); end def blue(text); colorize(text, 34); end def banner - ("Usage: #{Pathname($0).basename} [options] <search regex> [<replacement string>]" + $/) + - red("THIS PROGRAM COMES WITH NO WARRANTY WHATSOEVER. MAKE BACKUPS!") + red( "FINDR VERSION #{Findr::VERSION}. THIS PROGRAM COMES WITH NO WARRANTY WHATSOEVER. MAKE BACKUPS!") + $/ + + "Usage: #{Pathname($0).basename} [options] <search regex> [<replacement string>]" end def show_usage puts(@option_parser.help) exit @@ -36,24 +42,45 @@ # default options options = {} options[:glob] = '*' options[:coding] = 'utf-8' + # options from file, if present + if File.exists?( CONFIGFILE ) + file_options = YAML.load_file(CONFIGFILE) + file_options.delete(:save) + options.merge!( file_options ) + stdout.puts green "Using #{CONFIGFILE}." + end + + # parse command line @option_parser = OptionParser.new do |opts| opts.on('-g', '--glob FILE SEARCH GLOB', 'e.g. "*.{rb,erb}"') do |glob| options[:glob] = glob end opts.on('-x', '--execute', 'actually execute the replacement') do options[:force] = true end - opts.on('-c', '--coding FILE CODING SYSTEM', 'e.g. "latin-1"') do |coding| + opts.on('-c', '--coding FILE CODING SYSTEM', 'e.g. "iso-8859-1"') do |coding| options[:coding] = coding end + opts.on('-s', '--save', "saves your options to #{CONFIGFILE} for future use") do + options[:save] = true + end end @option_parser.banner = self.banner @option_parser.parse!( arguments ) + # optionally save the configuration to file + if options[:save] + options.delete(:save) + stdout.puts red "Nothing to save." unless options + File.open( CONFIGFILE, 'w' ) { |file| YAML::dump( options, file ) } + stdout.puts green "Saved options to file #{CONFIGFILE}." + exit + end + show_usage if arguments.size == 0 arguments.clone.each do |arg| if !options[:find] options[:find] = Regexp.compile( arg ) arguments.delete_at(0) @@ -62,12 +89,11 @@ arguments.delete_at(0) else show_usage end end - show_usage if arguments.size != 0 - + show_usage if arguments.size != 0 stdout.puts green "File inclusion glob: " + options[:glob] stdout.puts green "Searching for regex " + options[:find].to_s # some statistics stats = {}