forked from ahoward/clee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclee.rb
623 lines (491 loc) · 12.4 KB
/
clee.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# -*- encoding : utf-8 -*-
#
require_relative 'clee/_lib.rb'
Clee.load_dependencies!
#
class Clee
class Error < ::StandardError; end
attr_accessor :env
attr_accessor :argv
attr_accessor :options
attr_accessor :params
attr_accessor :stdin
attr_accessor :stdout
attr_accessor :stderr
attr_accessor :help
def _run!
_setup!
_parse_command_line!
_set_mode!
_run_mode!
end
def _setup!
@klass = self.class
@env = Hash.new
@options = Hash.new
@argv = ARGV.map(&:dup)
@stdin = $stdin.dup
@stdout = $stdout.dup
@stderr = $stderr.dup
@name = @klass.name.dup
@help = @klass.help.dup
@tldr = @klass.tldr.dup
end
def _parse_command_line!
_parse_options!
_parse_env!
_parse_params!
end
def _parse_options!
@options = Hash.new
o = OptionParser.new
klass.options.each do |spec|
spec => long:, short:, value:
args = []
case value
when :required
args.push "-#{ short }" if short
args.push "--#{ long } value" if long
when :optional
args.push "-#{ short }" if short
args.push "--#{ long } [value]" if long
when :none
args.push "-#{ short }" if short
args.push "--[no-]#{ long }" if long
end
o.on(*args) do |val|
if @options.has_key?(long)
@options[long] = [@options[long], val].flatten
else
@options[long] = val
end
end
end
begin
o.parse!(@argv)
rescue OptionParser::MissingArgument => e
warn(e.message)
exit 1
rescue OptionParser::InvalidOption => e
warn(e.message)
exit 1
end
Clee.symbolize_keys!(@options)
end
def _parse_env!
#
envs = Hash.new
@argv.each_with_index do |arg, index|
if arg =~ /[^=]+\s*=/
k, v = arg.split(/\s*=\s*/, 3)
key = k.to_s.strip.to_sym
val = v.to_s.strip == '' ? nil : v
envs[index] = {key:, val:}
end
end
#
validate = proc do |long:, value:, val:|
case value
when :required
unless val
warn("#{ long }=:missing")
exit 1
end
when :none
unless val.nil?
warn("#{ long }=#{ val }")
exit 1
end
when :optional
:noop
end
end
#
add_env = proc do |long, val|
if @env.has_key?(long)
@env[long] = [@env[long], val].flatten
else
@env[long] = val
end
end
klass.envs.each do |spec|
spec => long:, short:, keys:, value:
keys.reverse.each do |key|
if ENV.has_key?(key.to_s)
v = ENV[key.to_s]
val = v.to_s.strip == '' ? nil : v
validate[long:, value:, val:]
add_env[long, val]
end
end
envs.each do |index, env|
env => key:, val:
if([long, short].include?(key))
validate[long:, value:, val:]
add_env[long, val]
@argv[index] = nil
end
end
end
#
@argv.compact!
Clee.symbolize_keys!(@env)
end
def _parse_params!
@params = @env.merge(@options)
end
def _set_mode!
@mode = nil
first = 0
last = @argv.size
while last > 0
args = @argv[first ... last]
mode = klass.mode?(*args)
if mode
@mode = mode
@argv.replace(@argv[last..])
break
end
last = last - 1
end
end
def _run_mode!
if @mode
mode_method = klass.mode_method_for(@mode)
send(mode_method)
exit 0
end
if argv.first == 'help' || params[:help]
help!(exit: 0)
end
send(:run)
exit 0
end
def _default_run!
help!(exit: 1)
end
def run
_default_run!
end
def progname
File.basename($0)
end
def help!(**kws)
help = (@help || _default_help)
puts help
status = kws.fetch(:exit){ kws.fetch('exit'){ 1 } }
exit(status.to_i) if status
end
def _default_help
[].tap do |l|
p = proc do |string|
l.push string.to_s.rstrip
l.push "\n"
end
h = proc do |header|
l.push "\n"
p[header]
p['_' * header.to_s.size]
end
h[:NAME]
p[" #{ progname }"]
if @tldr
h[:TLDR]
p[" #{ @tldr }"]
end
unless klass.envs.empty?
h[:ENVIRONMENT]
klass.envs.each do |spec|
spec => value:, keys:
if value == :none
p[" #{ keys.join(' | ') }"]
else
p[" #{ keys.join(' | ') } : value=#{ value }"]
end
end
end
unless klass.options.empty?
h[:OPTIONS]
klass.options.each do |spec|
spec => value:, opts:
if value == :none
p[" #{ opts.join(' | ') }"]
else
p[" #{ opts.join(' | ') } : value=#{ value }"]
end
end
end
h[:MODES]
p[" ~> #{ progname }"]
modes.each do |mode|
p[" ~> #{ progname } #{ mode.join ' ' }"]
end
p[" ~> #{ progname } help"]
end.join.strip
end
#
def Clee.deep_copy(obj)
Marshal.load(Marshal.dump(obj))
end
def Clee.symbolize_keys!(hash)
hash.transform_keys!(&:to_sym)
hash.each do |key, val|
if val.is_a?(Hash)
symbolize_keys!(val)
end
end
hash
end
def Clee.stringify_keys(hash)
stringify_keys!(deep_copy(hash))
end
def Clee.stringify_keys!(hash)
hash.transform_keys!(&:to_sym)
hash.each do |key, val|
if val.is_a?(Hash)
stringify_keys!(val)
end
end
hash
end
def Clee.stringify_keys(hash)
stringify_keys!(deep_copy(hash))
end
#
def klass
self.class
end
def Clee.klass
self
end
#
ANSI = {
:clear => "\e[0m",
:reset => "\e[0m",
:erase_line => "\e[K",
:erase_char => "\e[P",
:bold => "\e[1m",
:dark => "\e[2m",
:underline => "\e[4m",
:underscore => "\e[4m",
:blink => "\e[5m",
:reverse => "\e[7m",
:concealed => "\e[8m",
:black => "\e[30m",
:red => "\e[31m",
:green => "\e[32m",
:yellow => "\e[33m",
:blue => "\e[34m",
:magenta => "\e[35m",
:cyan => "\e[36m",
:white => "\e[37m",
:on_black => "\e[40m",
:on_red => "\e[41m",
:on_green => "\e[42m",
:on_yellow => "\e[43m",
:on_blue => "\e[44m",
:on_magenta => "\e[45m",
:on_cyan => "\e[46m",
:on_white => "\e[47m"
}
Ansi = Object.new
ANSI.each do |key, value|
Ansi.singleton_class.define_method(key){ value }
end
def Clee.ansi
@ansi ||= Ansi
end
def ansi
klass.ansi
end
#
class Logger
Levels = [
:success,
:failure,
:message,
:warning,
:special,
]
Colors = {
success: Ansi.green,
failure: Ansi.red,
message: Ansi.cyan,
default: Ansi.blue,
warning: Ansi.yellow,
special: Ansi.magenta,
clear: Ansi.clear,
}
def initialize(io = $stderr)
@io = io
end
def log(arg, *args, **kws)
level = kws.fetch(:level){ :message }
color = kws[:color] ? Ansi.public_send(kws[:color]) : color_for(level)
clear = color_for(:clear)
[arg, *args].each do |arg|
ts = Time.now.utc.iso8601(2)
msg = msg_for(arg)
prefix = "### [#{ level.to_s.upcase } @ #{ ts }]"
if @io.tty?
@io.write(color)
@io.write(prefix)
@io.write(clear)
else
@io.write(prefix)
end
@io.write("\n#{ msg }\n")
@io.flush
end
end
Levels.each do |level|
define_method(level){|arg, *args| log(arg, *args, level:)}
end
def color_for(level)
Colors.fetch(level.to_s.to_sym){ Colors.fetch(:default) }
end
def msg_for(arg)
case
when arg.is_a?(String)
arg.strip
when arg.is_a?(Exception)
"#{ e.message } (#{ e.class.name })\n#{ Array(e.backtrace).join(10.chr) }"
else
arg.pretty_inspect
end
end
end
def Clee.logger
@@logger ||= Logger.new
end
def logger
klass.logger
end
def log(*args, **kws, &block)
return logger if args.empty? && kws.empty? && block.nil?
logger.message(*args, **kws, &block)
end
def emsg(e)
if e.is_a?(Exception)
"#{ e.message } (#{ e.class.name })\n#{ Array(e.backtrace).join(10.chr) }"
else
e.to_s
end
end
#
def Clee.parse_spec(list, *args, **kws, &block)
return list if args.empty? && kws.empty? && block.nil?
argv = []
args.each do |arg|
if arg.is_a?(Hash)
kws.update(Clee.symbolize_keys(arg))
else
argv.push(arg)
end
end
long = kws.fetch(:long){ argv.shift }
raise ArgumentError.new('long=nil') unless long
long = long.to_s.to_sym
short = kws.fetch(:short){ argv.shift }
value = kws.fetch(:value){ :none }.to_s.to_sym
values = [:none, :required, :optional]
raise ArgumentError.new("value=#{ value }") unless values.include?(value)
keys = [long, short].compact
opts = [(long && "--#{ long }"), (short &&"-#{ short}")].compact
spec = {long:, short:, value:, keys:, opts:}
list.push(spec).uniq!
end
def Clee.options(*args, **kws, &block)
parse_spec(@@options ||= [], *args, **kws, &block)
end
def Clee.option(*args, **kws, &block)
options(*args, **kws, &block)
end
def Clee.opt(*args, **kws, &block)
options(*args, **kws, &block)
end
def Clee.envs(*args, **kws, &block)
parse_spec(@@envs ||= [], *args, **kws, &block)
end
def Clee.env(*args, **kws, &block)
envs(*args, **kws, &block)
end
def Clee.params(*args, **kws, &block)
parse_spec(@@options ||= [], *args, **kws, &block)
parse_spec(@@envs ||= [], *args, **kws, &block)
@@options + @@envs
end
def Clee.param(*args, **kws, &block)
params(*args, **kws, &block)
end
def Clee.help(*args)
@help ||= nil
unless args.empty?
@help = args.join("\n")
end
@help
end
def Clee.tldr(*args)
@tldr ||= nil
unless args.empty?
@tldr = args.join("\n")
end
@tldr
end
def Clee.run(*args, **kws, &block)
if args.empty?
method = :run
else
mode = mode_for(*args)
klass.modes.push(mode).uniq!
method = mode_method_for(mode)
end
define_method(method, &block)
end
def Clee.mode_for(*args)
args.flatten!
args.compact!
return nil if args.empty?
args.map(&:to_s).map(&:to_sym)
end
def Clee.modes
@@modes ||= []
end
def modes
klass.modes
end
def Clee.mode?(*args)
mode = mode_for(*args)
return mode if modes.include?(mode)
end
def Clee.mode_method_for(mode)
"__mode__#{ [mode].join('__') }"
end
#
def Clee.klass_for(name = 'clee', &block)
Class.new(Clee).tap do |klass|
klass.class_eval do
define_singleton_method(:name){ "#{ name }" }
option(:help, :h, value: :none)
end
klass.class_eval(&block)
end
end
def Clee._run!(name = 'clee', *args, &block)
$stdout.sync = true
$stderr.sync = true
%w[ PIPE INT ].each{|signal| Signal.trap(signal, "EXIT")}
klass = Clee.klass_for(name, &block)
clee = klass.new
clee._run!(*args)
end
end
BEGIN {
Object.send(:remove_const, :Clee) if Object.const_defined?(:Clee)
def Clee(*args, &block)
clee(*args, &block)
end
def clee(name = 'clee', *args, &block)
Clee._run!(name, *args, &block)
end
}