Commit c84158cb4c9b3f91cc35e54afb8c12c8d9fb577a

Simplify and restructurize Options

* lib/amazing/options.rb: (Options) Inherit from Hash
instead of including enumerable.
(initialize) Move away aspects to private methods.
(each) Dropped, now inherited from Hash.
([]) Inherited from Hash.
([]=) Inherited from Hash.
(initialize_defaults) Set defaults on self here.
(initialize_parser) Set up the parser for self here.

Commit diff

lib/amazing/options.rb

 
66module Amazing
77
88 # Parse and manage command line options
9 class Options
10 include Enumerable
11
12 def initialize(args)
13 @options = {}
14 @options[:config] = Dir["#{ENV["HOME"]}/.amazing/config.{rb,yml,yaml}"][0]
15 @options[:loglevel] = "info"
16 @options[:include] = []
17 @options[:autoinclude] = true
18 @options[:update] = []
9 class Options < Hash
10 def initialize(args=ARGV)
1911 @args = args
12 initialize_defaults
13 initialize_parser
14 end
15
16 def parse(args=@args)
17 @parser.parse!(args)
18 end
19
20 def help
21 @parser.help
22 end
23
24 private
25
26 def initialize_defaults
27 self[:config] = Dir["#{ENV["HOME"]}/.amazing/config.{rb,yml,yaml}"][0]
28 self[:loglevel] = "info"
29 self[:include] = []
30 self[:autoinclude] = true
31 self[:update] = []
32 end
33
34 def initialize_parser
2035 @parser = OptionParser.new do |opts|
2136 opts.on("-c", "--config FILE", "Configuration file (~/.amazing/config.{rb,yml,yaml})") do |config|
22 @options[:config] = config
37 self[:config] = config
2338 end
39
2440 opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
25 @options[:loglevel] = level
41 self[:loglevel] = level
2642 end
43
2744 opts.on("-s", "--stop", "Stop the running amazing process") do
28 @options[:stop] = true
45 self[:stop] = true
2946 end
47
3048 opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
31 @options[:include] << script
49 self[:include] << script
3250 end
51
3352 opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do
34 @options[:autoinclude] = false
53 self[:autoinclude] = false
3554 end
55
3656 opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget|
3757 if widget
38 @options[:update] << widget
58 self[:update] << widget
3959 else
40 @options[:update] = :all
60 self[:update] = :all
4161 end
4262 end
63
4364 opts.on("-w", "--list-widgets [WIDGET]", "List available widgets or options and fields for a widget") do |widget|
44 @options[:listwidgets] = widget || true
65 self[:listwidgets] = widget || true
4566 end
67
4668 opts.on("-t", "--test-widget WIDGET [OPTIONS]", "Dump field values for a widget configured with inline YAML") do |widget|
47 @options[:test] = widget
69 self[:test] = widget
4870 end
71
4972 opts.on("-h", "--help", "You're looking at it") do
50 @options[:help] = true
73 self[:help] = true
5174 end
5275 end
5376 end
54
55 def each
56 @options.keys.each do |key|
57 yield key
58 end
59 end
60
61 def parse(args=@args)
62 @parser.parse!(args)
63 end
64
65 def help
66 @parser.help
67 end
68
69 def [](option)
70 @options[option]
71 end
72
73 def []=(option, value)
74 @options[option] = value
75 end
7677 end
7778end
toggle raw diff