Class: Cielli
- Inherits:
-
Object
show all
- Defined in:
- lib/cielli.rb,
lib/cielli/_lib.rb,
lib/cielli/slug.rb,
lib/cielli/utils.rb
Defined Under Namespace
Modules: Utils
Classes: Slug
Constant Summary
collapse
- EXAMPLE_HELP =
" NAME\n #TODO\n\n SYNOPSIS\n #TODO\n \n DESCRIPTION\n #TODO\n \n EXAMPLES\n #TODO\n"
- Version =
'4.2.4'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
29
30
31
|
# File 'lib/cielli.rb', line 29
def argv
@argv
end
|
#env ⇒ Object
Returns the value of attribute env.
28
29
30
|
# File 'lib/cielli.rb', line 28
def env
@env
end
|
#help ⇒ Object
Returns the value of attribute help.
33
34
35
|
# File 'lib/cielli.rb', line 33
def help
@help
end
|
#root ⇒ Object
Returns the value of attribute root.
27
28
29
|
# File 'lib/cielli.rb', line 27
def root
@root
end
|
#source ⇒ Object
Returns the value of attribute source.
26
27
28
|
# File 'lib/cielli.rb', line 26
def source
@source
end
|
#stderr ⇒ Object
Returns the value of attribute stderr.
32
33
34
|
# File 'lib/cielli.rb', line 32
def stderr
@stderr
end
|
#stdin ⇒ Object
Returns the value of attribute stdin.
31
32
33
|
# File 'lib/cielli.rb', line 31
def stdin
@stdin
end
|
#stdout ⇒ Object
Returns the value of attribute stdout.
30
31
32
|
# File 'lib/cielli.rb', line 30
def stdout
@stdout
end
|
Class Method Details
.after(&block) ⇒ Object
64
65
66
67
68
|
# File 'lib/cielli.rb', line 64
def self.after(&block)
@after ||= []
@after << block if block
@after
end
|
.before(&block) ⇒ Object
54
55
56
57
58
|
# File 'lib/cielli.rb', line 54
def self.before(&block)
@before ||= []
@before << block if block
@before
end
|
.dependencies ⇒ Object
13
14
15
|
# File 'lib/cielli/_lib.rb', line 13
def dependencies
{ }
end
|
.help(*args) ⇒ Object
167
168
169
170
171
172
173
174
175
|
# File 'lib/cielli.rb', line 167
def Cielli.help(*args)
@help ||= nil
unless args.empty?
@help = utils.unindent(args.join)
end
@help
end
|
.klass_for(&block) ⇒ Object
198
199
200
201
202
203
|
# File 'lib/cielli.rb', line 198
def Cielli.klass_for(&block)
Class.new(Cielli) do |klass|
def klass.name; "Cielli::Klass__#{ SecureRandom.uuid.to_s.gsub('-', '_') }"; end
klass.class_eval(&block)
end
end
|
.libdir(*args, &block) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/cielli/_lib.rb', line 30
def libdir(*args, &block)
@libdir ||= File.dirname(File.expand_path(__FILE__).sub(/\.rb$/,''))
args.empty? ? @libdir : File.join(@libdir, *args)
ensure
if block
begin
$LOAD_PATH.unshift(@libdir)
block.call()
ensure
$LOAD_PATH.shift()
end
end
end
|
.load(*libs) ⇒ Object
44
45
46
47
|
# File 'lib/cielli/_lib.rb', line 44
def load(*libs)
libs = libs.join(' ').scan(/[^\s+]+/)
libdir{ libs.each{|lib| Kernel.load(lib) } }
end
|
.load_dependencies! ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/cielli/_lib.rb', line 17
def load_dependencies!
begin
require 'rubygems'
rescue LoadError
nil
end
dependencies.each do |lib, dependency|
gem(*dependency) if defined?(gem)
require(lib)
end
end
|
.run(*args, &block) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/cielli.rb', line 177
def Cielli.run(*args, &block)
modes =
if args.empty?
[nil]
else
args
end
modes.each do |mode|
method_name =
if mode
"run_#{ mode }"
else
"run"
end
define_method(method_name, &block)
end
end
|
.run!(*args, &block) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/cielli.rb', line 205
def Cielli.run!(*args, &block)
STDOUT.sync = true
STDERR.sync = true
%w[ PIPE INT ].each{|signal| Signal.trap(signal, "EXIT")}
cielli = (
source =
if binding.respond_to?(:source_location)
File.expand_path(binding.source_location.first)
else
File.expand_path(eval('__FILE__', block.binding))
end
root = File.dirname(source)
klass = Cielli.klass_for(&block)
instance = klass.new
instance.source = source
instance.root = root
instance
)
cielli.run!(*args)
end
|
.summary ⇒ Object
9
10
11
|
# File 'lib/cielli/_lib.rb', line 9
def summary
"a minimalist's toolkit for quickly writing well behaved CLI/cee-el-ahy programs"
end
|
.u(&block) ⇒ Object
243
244
245
|
# File 'lib/cielli.rb', line 243
def Cielli.u(&block)
Cielli.utils(&block)
end
|
.utils(&block) ⇒ Object
239
240
241
|
# File 'lib/cielli.rb', line 239
def Cielli.utils(&block)
block ? Cielli::Utils.module_eval(&block) : Cielli::Utils
end
|
.version ⇒ Object
5
6
7
|
# File 'lib/cielli/_lib.rb', line 5
def version
Version
end
|
Instance Method Details
#after ⇒ Object
70
71
72
|
# File 'lib/cielli.rb', line 70
def after
self.class.after.each{|block| block.call}
end
|
#before ⇒ Object
60
61
62
|
# File 'lib/cielli.rb', line 60
def before
self.class.before.each{|block| block.call}
end
|
#help! ⇒ Object
161
162
163
164
|
# File 'lib/cielli.rb', line 161
def help!
run_help!
abort
end
|
#init!(env, argv) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/cielli.rb', line 44
def init!(env, argv)
@klass = self.class
@env = env.to_hash.dup
@argv = argv.map{|arg| arg.dup}
@stdout = $stdout.dup
@stdin = $stdin.dup
@stderr = $stderr.dup
@help = @klass.help || utils.unindent(EXAMPLE_HELP)
end
|
#parse_command_line! ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/cielli.rb', line 88
def parse_command_line!
@options = Hash.new
@opts = Hash.new
argv = []
head = []
tail = []
%w[ :: -- ].each do |stop|
if((i = @argv.index(stop)))
head = @argv.slice(0 ... i)
tail = @argv.slice((i + 1) ... @argv.size)
@argv = head
break
end
end
@argv.each do |arg|
case
when arg =~ %r`^\s*:([^:\s]+)[=](.+)`
key = $1
val = $2
@options[key] = val
when arg =~ %r`^\s*(:+)(.+)`
leader = $1
key = $2
val = leader.size.odd?
@options[key] = val
else
argv.push(arg)
end
end
argv += tail
@argv.replace(argv)
@options.each do |key, val|
@opts[key.to_s.to_sym] = val
end
[@options, @opts]
end
|
#run!(env = ENV, argv = ARGV) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/cielli.rb', line 35
def run!(env = ENV, argv = ARGV)
before!
init!(env, argv)
parse_command_line!
set_mode!
run_mode!
after!
end
|
#run_help! ⇒ Object
157
158
159
|
# File 'lib/cielli.rb', line 157
def run_help!
STDOUT.puts(@help)
end
|
#run_mode! ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/cielli.rb', line 141
def run_mode!
if @mode
return send("run_#{ @mode }")
else
if respond_to?(:run)
return send(:run)
end
if @argv.empty?
run_help!
else
abort("#{ $0 } help")
end
end
end
|
#set_mode! ⇒ Object
132
133
134
135
136
137
138
139
|
# File 'lib/cielli.rb', line 132
def set_mode!
case
when respond_to?("run_#{ @argv[0] }")
@mode = @argv.shift
else
@mode = nil
end
end
|