Auto inclusion
[amazing.git] / lib / amazing / options.rb
blob94d9d0837fbbe0306e1463b8c718231445c27caf
1 # Copyright (C) 2008 Dag Odenhall <dag.odenhall@gmail.com>
2 # Licensed under the Academic Free License version 3.0
4 require 'optparse'
6 module Amazing
8   # Parse and manage command line options
9   class Options
10     include Enumerable
12     def initialize(args)
13       @options = {}
14       @options[:config] = "#{ENV["HOME"]}/.amazing.yml"
15       @options[:loglevel] = "info"
16       @options[:include] = []
17       @options[:autoinclude] = true
18       @options[:update] = []
19       @args = args
20       @parser = OptionParser.new do |opts|
21         opts.on("-c", "--config FILE", "Configuration file (~/.amazing.yml)") do |config|
22           @options[:config] = config
23         end
24         opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
25           @options[:loglevel] = level
26         end
27         opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
28           @options[:include] << script
29         end
30         opts.on("--no-auto-include", "Don't auto include from ~/.amazing/*.rb") do
31           @options[:autoinclude] = false
32         end
33         opts.on("-u", "--update WIDGET", "Update a widget and exit") do |widget|
34           @options[:update] << widget
35         end
36         opts.on("-w", "--list-widgets", "List available widgets") do
37           @options[:listwidgets] = true
38         end
39         opts.on("-h", "--help", "You're looking at it") do
40           @options[:help] = true
41         end
42       end
43     end
45     def each
46       @options.keys.each do |key|
47         yield key
48       end
49     end
51     def parse(args=@args)
52       @parser.parse!(args)
53     end
55     def help
56       @parser.help
57     end
59     def [](option)
60       @options[option]
61     end
63     def []=(option, value)
64       @options[option] = value
65     end
66   end
67 end