Drop YAML completely
[amazing.git] / lib / amazing / options.rb
blob58f42578e9eac6a4bdc493f250625dd6b5d035f7
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"][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)") do |config|
48           self[:config] = config
49         end
51         opts.on("-g", "--scaffold [AWESOMERC]", "Generate a scaffold config") do |awesomerc|
52           self[:scaffold] = awesomerc || File.expand_path("~/.awesomerc")
53         end
55         opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
56           self[:loglevel] = level
57         end
59         opts.on("-s", "--stop", "Stop the running amazing process") do
60           self[:stop] = true
61         end
63         opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
64           self[:include] << script
65         end
67         opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do
68           self[:autoinclude] = false
69         end
71         opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget|
72           if widget
73             self[:update] << widget
74           else
75             self[:update] = :all
76           end
77         end
79         opts.on("-w", "--list-widgets [WIDGET]", "List available widgets or options and fields for a widget") do |widget|
80           self[:listwidgets] = widget || true
81         end
83         opts.on("-t", "--test-widget WIDGET [OPTIONS]", "Dump field values for a widget") do |widget|
84           self[:test] = widget
85         end
87         opts.on("-h", "--help", "You're looking at it") do
88           self[:help] = true
89         end
90       end
91     end
92   end
93 end