Class: Ro::Script
Defined Under Namespace
Classes: Builder, Console, Server
Constant Summary
collapse
- @@SAY =
{
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"
}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
Returns the value of attribute argv.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def argv
@argv
end
|
Returns the value of attribute cls.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def cls
@cls
end
|
Returns the value of attribute env.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def env
@env
end
|
Returns the value of attribute help.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def help
@help
end
|
Returns the value of attribute mode.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def mode
@mode
end
|
Returns the value of attribute options.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def options
@options
end
|
Returns the value of attribute stderr.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def stderr
@stderr
end
|
Returns the value of attribute stdin.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def stdin
@stdin
end
|
Returns the value of attribute stdout.
7
8
9
|
# File 'lib/ro/script.rb', line 7
def stdout
@stdout
end
|
Class Method Details
.help(*args) ⇒ Object
155
156
157
158
|
# File 'lib/ro/script.rb', line 155
def self.help(*args)
@help = args.join("\n") unless args.empty?
@help
end
|
.run(*args, &block) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/ro/script.rb', line 160
def self.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!(&block) ⇒ Object
180
181
182
183
184
185
186
187
|
# File 'lib/ro/script.rb', line 180
def self.run!(&block)
STDOUT.sync = true
STDERR.sync = true
%w[PIPE INT].each { |signal| Signal.trap(signal, 'EXIT') }
Class.new(Script, &block).new.run!(ENV, ARGV)
end
|
Instance Method Details
#err(*messages) ⇒ Object
98
99
100
|
# File 'lib/ro/script.rb', line 98
def err(*messages)
log(*messages, io: @stderr)
end
|
79
80
81
82
|
# File 'lib/ro/script.rb', line 79
def help!
@stderr.puts(@help)
abort
end
|
#initialize!(env, argv) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/ro/script.rb', line 15
def initialize!(env, argv)
@cls = self.class
@env = env.to_hash.dup
@argv = argv.map { |arg| arg.dup }
@options = {}
@mode = nil
@stdout = $stdout.dup
@stdin = $stdin.dup
@stderr = $stderr.dup
@help = @cls.help
end
|
#log(*messages, io: $stdout) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/ro/script.rb', line 84
def log(*messages, io: $stdout)
ts = Time.now.utc.iso8601
prefix = File.basename($0)
msg = messages.join("\n\s\s").strip
io.write "---\n[#{prefix}@#{ts}]\n\s\s#{msg}\n\n\n"
begin
io.flush
rescue StandardError
nil
end
end
|
#parse_command_line! ⇒ Object
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
|
# File 'lib/ro/script.rb', line 27
def parse_command_line!
argv = []
head = []
tail = []
%w[--].each do |stop|
next unless (i = @argv.index(stop))
head = @argv.slice(0...i)
tail = @argv.slice((i + 1)...@argv.size)
@argv = head
break
end
@argv.each do |arg|
if arg =~ /^\s*--([^\s-]+)=(.+)/
key = ::Regexp.last_match(1)
val = ::Regexp.last_match(2)
@options[key.to_sym] = val
elsif arg =~ /^\s*(-+)(.+)/
switch = ::Regexp.last_match(1)
key = ::Regexp.last_match(2)
val = switch.size.even?
@options[key.to_sym] = val
else
argv.push(arg)
end
end
argv += tail
@argv.replace(argv)
@mode = (@argv.shift if respond_to?("run_#{@argv[0]}!"))
end
|
71
72
73
|
# File 'lib/ro/script.rb', line 71
def run
help!
end
|
#run!(env = ENV, argv = ARGV) ⇒ Object
9
10
11
12
13
|
# File 'lib/ro/script.rb', line 9
def run!(env = ENV, argv = ARGV)
initialize!(env, argv)
parse_command_line!
run_mode!
end
|
#run_help! ⇒ Object
75
76
77
|
# File 'lib/ro/script.rb', line 75
def run_help!
@stdout.puts(@help)
end
|
#run_mode! ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/ro/script.rb', line 63
def run_mode!
if @mode
send("run_#{@mode}!")
else
run
end
end
|
#say(phrase, *args) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/ro/script.rb', line 132
def say(phrase, *args)
options = args.last.is_a?(Hash) ? args.pop : {}
options[:color] = args.shift.to_s.to_sym unless args.empty?
keys = options.keys
keys.each { |key| options[key.to_s.to_sym] = options.delete(key) }
color = options[:color]
bold = options.has_key?(:bold)
parts = [phrase]
parts.unshift(@@SAY[color]) if color
parts.unshift(@@SAY[:bold]) if bold
parts.push(@@SAY[:clear]) if parts.size > 1
method = options[:method] || :puts
if STDOUT.tty?
::Kernel.send(method, parts.join)
else
::Kernel.send(method, phrase)
end
end
|