Typo in expand_path in Maildir
[amazing.git] / lib / amazing / options.rb
blobcf501a64f79b3642aa8dfa7254d9c93beba2297f
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
21     include Enumerable
23     def initialize(args)
24       @options = {}
25       @options[:config] = "#{ENV["HOME"]}/.amazing/config.yml"
26       @options[:loglevel] = "info"
27       @options[:include] = []
28       @options[:autoinclude] = true
29       @options[:update] = []
30       @args = args
31       @parser = OptionParser.new do |opts|
32         opts.on("-c", "--config FILE", "Configuration file (~/.amazing/config.yml)") do |config|
33           @options[:config] = config
34         end
35         opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
36           @options[:loglevel] = level
37         end
38         opts.on("-s", "--stop", "Stop the running amazing process") do
39           @options[:stop] = true
40         end
41         opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
42           @options[:include] << script
43         end
44         opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do
45           @options[:autoinclude] = false
46         end
47         opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget|
48           if widget
49             @options[:update] << widget
50           else
51             @options[:update] = :all
52           end
53         end
54         opts.on("-w", "--list-widgets [WIDGET]", "List available widgets or options and fields for a widget") do |widget|
55           @options[:listwidgets] = widget || true
56         end
57         opts.on("-t", "--test-widget WIDGET [OPTIONS]", "Dump field values for a widget configured with inline YAML") do |widget|
58           @options[:test] = widget
59         end
60         opts.on("-h", "--help", "You're looking at it") do
61           @options[:help] = true
62         end
63       end
64     end
66     def each
67       @options.keys.each do |key|
68         yield key
69       end
70     end
72     def parse(args=@args)
73       @parser.parse!(args)
74     end
76     def help
77       @parser.help
78     end
80     def [](option)
81       @options[option]
82     end
84     def []=(option, value)
85       @options[option] = value
86     end
87   end
88 end