ruby-nuggets 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/nuggets/util/cli.rb +209 -0
- data/lib/nuggets/version.rb +1 -1
- metadata +9 -8
data/README
CHANGED
@@ -0,0 +1,209 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU Affero General Public License as published by #
|
14
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
+
# option) any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
+
# for more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU Affero General Public License #
|
23
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'optparse'
|
29
|
+
require 'yaml'
|
30
|
+
require 'zlib'
|
31
|
+
require 'highline'
|
32
|
+
|
33
|
+
module Util
|
34
|
+
|
35
|
+
class CLI
|
36
|
+
|
37
|
+
class << self
|
38
|
+
|
39
|
+
def usage(prog)
|
40
|
+
"Usage: #{prog} [-h|--help] [options]"
|
41
|
+
end
|
42
|
+
|
43
|
+
def version
|
44
|
+
parent_const_get(:VERSION)
|
45
|
+
end
|
46
|
+
|
47
|
+
def defaults
|
48
|
+
{}
|
49
|
+
end
|
50
|
+
|
51
|
+
def execute(*args)
|
52
|
+
new.execute(*args)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def parent_const_get(const, range = 0...-1)
|
58
|
+
name.split('::').inject([Object]) { |memo, name|
|
59
|
+
memo << memo.last.const_get(name)
|
60
|
+
}.reverse[range].each { |mod|
|
61
|
+
return mod.const_get(const) if mod.const_defined?(const)
|
62
|
+
}
|
63
|
+
|
64
|
+
raise NameError, "uninitialized constant #{self}::#{const}"
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
attr_reader :options, :config, :defaults
|
70
|
+
attr_reader :stdin, :stdout, :stderr
|
71
|
+
|
72
|
+
attr_accessor :prog
|
73
|
+
|
74
|
+
def initialize(defaults = nil, *args)
|
75
|
+
@defaults, @prog = defaults || self.class.defaults, $0
|
76
|
+
|
77
|
+
init(*args)
|
78
|
+
|
79
|
+
# prevent backtrace on ^C
|
80
|
+
trap(:INT) { exit 130 }
|
81
|
+
end
|
82
|
+
|
83
|
+
def progname
|
84
|
+
File.basename(prog)
|
85
|
+
end
|
86
|
+
|
87
|
+
def usage
|
88
|
+
self.class.usage(prog)
|
89
|
+
end
|
90
|
+
|
91
|
+
def version
|
92
|
+
self.class.version
|
93
|
+
end
|
94
|
+
|
95
|
+
def execute(arguments = ARGV, *inouterr)
|
96
|
+
reset(*inouterr)
|
97
|
+
parse_options(arguments)
|
98
|
+
run(arguments)
|
99
|
+
rescue => err
|
100
|
+
raise if $VERBOSE
|
101
|
+
abort "#{err.backtrace.first}: #{err} (#{err.class})"
|
102
|
+
ensure
|
103
|
+
options.each_value { |value|
|
104
|
+
value.close if value.is_a?(Zlib::GzipWriter)
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
def reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR)
|
109
|
+
@stdin, @stdout, @stderr = stdin, stdout, stderr
|
110
|
+
@options, @config = {}, {}
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def init(*args)
|
116
|
+
reset
|
117
|
+
end
|
118
|
+
|
119
|
+
def ask(question, &block)
|
120
|
+
HighLine.new(stdin, stdout).ask(question, &block)
|
121
|
+
end
|
122
|
+
|
123
|
+
def warn(msg)
|
124
|
+
stderr.puts(msg)
|
125
|
+
end
|
126
|
+
|
127
|
+
def quit(msg = nil, include_usage = msg != false)
|
128
|
+
out = []
|
129
|
+
|
130
|
+
out << "#{progname}: #{msg}" if msg
|
131
|
+
out << usage if include_usage
|
132
|
+
|
133
|
+
abort out.any? && out.join("\n\n")
|
134
|
+
end
|
135
|
+
|
136
|
+
def abort(msg = nil, status = 1)
|
137
|
+
warn(msg) if msg
|
138
|
+
exit(status)
|
139
|
+
end
|
140
|
+
|
141
|
+
def exit(status = 0)
|
142
|
+
Kernel.exit(status)
|
143
|
+
end
|
144
|
+
|
145
|
+
def open_file_or_std(file, write = false)
|
146
|
+
if file == '-'
|
147
|
+
write ? stdout : stdin
|
148
|
+
else
|
149
|
+
gz = file =~ /\.gz\z/i
|
150
|
+
|
151
|
+
if write
|
152
|
+
gz ? Zlib::GzipWriter.open(file) : File.open(file, 'w')
|
153
|
+
else
|
154
|
+
quit "No such file: #{file}" unless File.readable?(file)
|
155
|
+
(gz ? Zlib::GzipReader : File).open(file)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def parse_options(arguments)
|
161
|
+
option_parser.parse!(arguments)
|
162
|
+
|
163
|
+
config_file = options[:config] || defaults[:config]
|
164
|
+
@config = YAML.load_file(config_file) if File.readable?(config_file)
|
165
|
+
|
166
|
+
[config, defaults].each { |hash| hash.each { |key, value| options[key] ||= value } }
|
167
|
+
end
|
168
|
+
|
169
|
+
def option_parser
|
170
|
+
OptionParser.new { |opts|
|
171
|
+
opts.banner = usage
|
172
|
+
|
173
|
+
pre_opts(opts)
|
174
|
+
|
175
|
+
opts.separator ''
|
176
|
+
opts.separator 'Options:'
|
177
|
+
|
178
|
+
opts(opts)
|
179
|
+
|
180
|
+
opts.separator ''
|
181
|
+
opts.separator 'Generic options:'
|
182
|
+
|
183
|
+
generic_opts(opts)
|
184
|
+
post_opts(opts)
|
185
|
+
}
|
186
|
+
end
|
187
|
+
|
188
|
+
def pre_opts(opts)
|
189
|
+
end
|
190
|
+
|
191
|
+
def opts(opts)
|
192
|
+
end
|
193
|
+
|
194
|
+
def generic_opts(opts)
|
195
|
+
opts.on('-h', '--help', 'Print this help message and exit') {
|
196
|
+
abort opts.to_s
|
197
|
+
}
|
198
|
+
|
199
|
+
opts.on('--version', 'Print program version and exit') {
|
200
|
+
abort "#{progname} v#{version}"
|
201
|
+
}
|
202
|
+
end
|
203
|
+
|
204
|
+
def post_opts(opts)
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
data/lib/nuggets/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 3
|
10
|
+
version: 0.7.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jens Wille
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-07-14 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Some extensions to the Ruby programming language.
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/nuggets/util/i18n.rb
|
41
41
|
- lib/nuggets/util/ruby.rb
|
42
42
|
- lib/nuggets/util/dotted_decimal.rb
|
43
|
+
- lib/nuggets/util/cli.rb
|
43
44
|
- lib/nuggets/util/content_type.rb
|
44
45
|
- lib/nuggets/util/ansicolor2css.rb
|
45
46
|
- lib/nuggets/util/added_methods.rb
|
@@ -191,14 +192,14 @@ licenses: []
|
|
191
192
|
|
192
193
|
post_install_message:
|
193
194
|
rdoc_options:
|
195
|
+
- --title
|
196
|
+
- ruby-nuggets Application documentation (v0.7.3)
|
194
197
|
- --line-numbers
|
195
198
|
- --main
|
196
199
|
- README
|
200
|
+
- --all
|
197
201
|
- --charset
|
198
202
|
- UTF-8
|
199
|
-
- --title
|
200
|
-
- ruby-nuggets Application documentation (v0.7.2)
|
201
|
-
- --all
|
202
203
|
require_paths:
|
203
204
|
- lib
|
204
205
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -222,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
222
223
|
requirements: []
|
223
224
|
|
224
225
|
rubyforge_project: prometheus
|
225
|
-
rubygems_version: 1.
|
226
|
+
rubygems_version: 1.8.5
|
226
227
|
signing_key:
|
227
228
|
specification_version: 3
|
228
229
|
summary: Some extensions to the Ruby programming language.
|