Merge branch 'master' into config
[amazing.git] / lib / amazing / options.rb
blob0c4175c1c39dd30ec227ff46d7066c513c276673
1 # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #    http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require 'optparse'
17 module Amazing
19   # Parse and manage command line options
20   class Options < Hash
21     def initialize(args=ARGV)
22       @args = args
23       initialize_defaults
24       initialize_parser
25     end
27     def parse(args=@args)
28       @parser.parse!(args)
29     end
31     def help
32       @parser.help
33     end
35     private
37     def initialize_defaults
38       self[:config] = Dir["#{ENV["HOME"]}/.amazing/config.{rb,yml,yaml}"][0]
39       self[:loglevel] = "info"
40       self[:include] = []
41       self[:autoinclude] = true
42       self[:update] = []
43     end
45     def initialize_parser
46       @parser = OptionParser.new do |opts|
47         opts.on("-c", "--config FILE", "Configuration file (~/.amazing/config.{rb,yml,yaml})") do |config|
48           self[:config] = config
49         end
51         opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
52           self[:loglevel] = level
53         end
55         opts.on("-s", "--stop", "Stop the running amazing process") do
56           self[:stop] = true
57         end
59         opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
60           self[:include] << script
61         end
63         opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do
64           self[:autoinclude] = false
65         end
67         opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget|
68           if widget
69             self[:update] << widget
70           else
71             self[:update] = :all
72           end
73         end
75         opts.on("-w", "--list-widgets [WIDGET]", "List available widgets or options and fields for a widget") do |widget|
76           self[:listwidgets] = widget || true
77         end
79         opts.on("-t", "--test-widget WIDGET [OPTIONS]", "Dump field values for a widget configured with inline YAML") do |widget|
80           self[:test] = widget
81         end
83         opts.on("-h", "--help", "You're looking at it") do
84           self[:help] = true
85         end
86       end
87     end
88   end
89 end