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.
| |   |
| 6 | 6 | module Amazing |
| 7 | 7 | |
| 8 | 8 | # 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) |
| 19 | 11 | @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 |
| 20 | 35 | @parser = OptionParser.new do |opts| |
| 21 | 36 | opts.on("-c", "--config FILE", "Configuration file (~/.amazing/config.{rb,yml,yaml})") do |config| |
| 22 | | @options[:config] = config |
| 37 | self[:config] = config |
| 23 | 38 | end |
| 39 | |
| 24 | 40 | opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level| |
| 25 | | @options[:loglevel] = level |
| 41 | self[:loglevel] = level |
| 26 | 42 | end |
| 43 | |
| 27 | 44 | opts.on("-s", "--stop", "Stop the running amazing process") do |
| 28 | | @options[:stop] = true |
| 45 | self[:stop] = true |
| 29 | 46 | end |
| 47 | |
| 30 | 48 | opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script| |
| 31 | | @options[:include] << script |
| 49 | self[:include] << script |
| 32 | 50 | end |
| 51 | |
| 33 | 52 | opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do |
| 34 | | @options[:autoinclude] = false |
| 53 | self[:autoinclude] = false |
| 35 | 54 | end |
| 55 | |
| 36 | 56 | opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget| |
| 37 | 57 | if widget |
| 38 | | @options[:update] << widget |
| 58 | self[:update] << widget |
| 39 | 59 | else |
| 40 | | @options[:update] = :all |
| 60 | self[:update] = :all |
| 41 | 61 | end |
| 42 | 62 | end |
| 63 | |
| 43 | 64 | 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 |
| 45 | 66 | end |
| 67 | |
| 46 | 68 | 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 |
| 48 | 70 | end |
| 71 | |
| 49 | 72 | opts.on("-h", "--help", "You're looking at it") do |
| 50 | | @options[:help] = true |
| 73 | self[:help] = true |
| 51 | 74 | end |
| 52 | 75 | end |
| 53 | 76 | 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 |
| 76 | 77 | end |
| 77 | 78 | end |
| toggle raw diff |
--- a/lib/amazing/options.rb
+++ b/lib/amazing/options.rb
@@ -6,72 +6,73 @@ require 'optparse'
module Amazing
# Parse and manage command line options
- class Options
- include Enumerable
-
- def initialize(args)
- @options = {}
- @options[:config] = Dir["#{ENV["HOME"]}/.amazing/config.{rb,yml,yaml}"][0]
- @options[:loglevel] = "info"
- @options[:include] = []
- @options[:autoinclude] = true
- @options[:update] = []
+ class Options < Hash
+ def initialize(args=ARGV)
@args = args
+ initialize_defaults
+ initialize_parser
+ end
+
+ def parse(args=@args)
+ @parser.parse!(args)
+ end
+
+ def help
+ @parser.help
+ end
+
+ private
+
+ def initialize_defaults
+ self[:config] = Dir["#{ENV["HOME"]}/.amazing/config.{rb,yml,yaml}"][0]
+ self[:loglevel] = "info"
+ self[:include] = []
+ self[:autoinclude] = true
+ self[:update] = []
+ end
+
+ def initialize_parser
@parser = OptionParser.new do |opts|
opts.on("-c", "--config FILE", "Configuration file (~/.amazing/config.{rb,yml,yaml})") do |config|
- @options[:config] = config
+ self[:config] = config
end
+
opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
- @options[:loglevel] = level
+ self[:loglevel] = level
end
+
opts.on("-s", "--stop", "Stop the running amazing process") do
- @options[:stop] = true
+ self[:stop] = true
end
+
opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
- @options[:include] << script
+ self[:include] << script
end
+
opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do
- @options[:autoinclude] = false
+ self[:autoinclude] = false
end
+
opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget|
if widget
- @options[:update] << widget
+ self[:update] << widget
else
- @options[:update] = :all
+ self[:update] = :all
end
end
+
opts.on("-w", "--list-widgets [WIDGET]", "List available widgets or options and fields for a widget") do |widget|
- @options[:listwidgets] = widget || true
+ self[:listwidgets] = widget || true
end
+
opts.on("-t", "--test-widget WIDGET [OPTIONS]", "Dump field values for a widget configured with inline YAML") do |widget|
- @options[:test] = widget
+ self[:test] = widget
end
+
opts.on("-h", "--help", "You're looking at it") do
- @options[:help] = true
+ self[:help] = true
end
end
end
-
- def each
- @options.keys.each do |key|
- yield key
- end
- end
-
- def parse(args=@args)
- @parser.parse!(args)
- end
-
- def help
- @parser.help
- end
-
- def [](option)
- @options[option]
- end
-
- def []=(option, value)
- @options[option] = value
- end
end
end |