class Empyrean::OptParser

Public Class Methods

parse(args) click to toggle source
# File lib/empyrean/optparser.rb, line 27
def self.parse(args)
  options = OpenStruct.new
  options.jsondir = ""
  options.outfile = "output.html"
  options.config = File.expand_path('.', "config.yml")
  options.config_values = {}
  options.print_stats = false
  options.template = DEFAULT_TEMPLATE
  options.verbose = false

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: stats.rb [options]"

    opts.separator ""
    opts.separator "Specific options:"
    
    opts.on("-c", "--config CONFIG", "The configuration file to use (default: #{options.config})") do |config|
      options.config = config
    end

    opts.on("-C", "--config-value KEY=VALUE", "Sets a configuration value (e.g. hashtags_enabled=false)") do |val|
      key, value = val.split("=")
      key = key.downcase
      unless value.nil?   # ignore empty values
        case key.to_s
        when /_enabled$/
          value = to_bool value
        when /_(no)?top$/, /timezone_difference$/
          value = value.to_i
        when /ignored_users/
          value = value.split ','
        end
        options.config_values[key.to_sym] = value
      end
    end

    opts.on("-g", "--generate-config CONFIG", "Generate a new configuration file") do |config_file|
      fn = File.expand_path '.', config_file
      if File.exist? fn
        STDERR.puts "Cowardly refusing to overwrite already existing file #{config_file}."
        exit 2
      end
      File.open fn, 'w' do |f|
        f.write File.read File.expand_path("../../../config.yml.example", __FILE__)
        STDERR.puts "Wrote new configuration to #{config_file}."
        exit
      end
    end

    opts.on("-d", "--jsondir DIRECTORY", "Directory with tweet files (containing 2014_07.js etc.)") do |dir|
      dir = File.expand_path('.', dir)
      unless File.exist?(dir) && File.directory?(dir)
        STDERR.puts "not a directory: #{dir}"
        exit 1
      end

      entries = Dir.entries(dir)
      entries.each do |e|
        # the file names of the tweet archive are in the format /^\d{4}_\d{2}\.js$/
        unless e =~ /^\d{4}_\d{2}\.js$|\.+?/
          STDERR.puts "not a tweets directory: #{dir}"
          exit 1
        end
      end

      options.jsondir = dir
    end

    opts.on("-o", "--outfile OUTFILE", "Output HTML file (default: #{options.outfile})") do |outfile|
      options.outfile = outfile
    end
    
    opts.on("-l", "--list-templates", "List available templates") do
      TemplateLister.print_list
      exit
    end

    opts.on("-t", "--template TEMPLATE", "Template to use (default: #{options.template})") do |template|
      options.template = template
    end

    opts.on("-p", "--[no-]print-stats", "Print stats to stdout") do |p|
      options.print_stats = p
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end
    
    # No argument, shows at tail.  This will print an options summary.
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail("--version", "Show version") do
      puts Empyrean::VERSION_STR
      exit
    end
  end.parse!(args)

  if options.jsondir.empty?
    STDERR.puts "missing argument: --jsondir, see --help for more"
    exit 1
  end

  options
end

Private Class Methods

to_bool(str) click to toggle source

“converts” a string to a boolean value

# File lib/empyrean/optparser.rb, line 141
def self.to_bool str
  if str == true || str =~ (/(true|t|yes|y|1)$/i)
    true
  else
    false
  end
end