Class: Empyrean::OptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/empyrean/optparser.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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