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
|
# File 'lib/rube/cli.rb', line 13
def self.execute(stdout, arguments=[])
options = {
:tasks => [],
:explicit => false,
:trim_level => '0',
:disable_percent => false,
:safe => nil,
:from_command_line => true
}
mandatory_options = %w( )
template_count = 0
tr_level = nil
parser = OptionParser.new do |opts|
opts = OptionParser.new
opts.banner = <<-BANNER.gsub(/^ /,'')
Process erb templates along with other ruby tasks
Usage: #{File.basename($0)} [options] tasks
Each task may be a template, require or eval (see below). These are processed in the order given,
so results from prior tasks are available to subsequent ones. All variables and constants, including
local variables, are preserved, so their values are available to subsequent tasks.
BANNER
opts.separator ''
opts.separator "Tasks:"
opts.separator " path/to/template/file Process the specified erb template file"
opts.on('-i', '--stdin', "Process the template provided in stdin") do |val|
template_count += 1
options[:tasks] << [:template, '/dev/stdin']
end
opts.on('-r', '--require path/to/ruby/file', "Load a ruby library or source code file") {|val| options[:tasks] << [:require, val] }
opts.on('-e', '--eval "ruby code"', "Evaluate some inline ruby code"){|src| options[:tasks] << [:eval, src] }
opts.separator ''
opts.separator "Options:"
opts.on('-E', '--[no-]explicit', "All templates must be explicitly provided. Default is false -- rube assumes it should read",
"a template from stdin if no templates are specified among the tasks") {|val| options[:explicit] = val }
opts.on('-S', '--safe SAFE_LEVEL', Integer, "Set $SAFE (0..4). Default off") do |val|
help stdout, opts, "Invalid --safe level #{val}. Should be 0..4", ExitStatus::BAD_ARGUMENT unless (0..4).include?(val)
options[:safety] = val
end
opts.on('-T', '--trim TRIM_LEVEL', "Set trim level (0..2, or '-'). Default 0") {|trim| tr_level = trim }
opts.on('-P', '--[no-]disable-percent', "Disable '%' prefix for erb code. Default false") {|val| options[:disable_percent] = val }
opts.on_tail('-h', '--help', "Produce this help list") {|val| help stdout, opts }
opts.on_tail('-v', '--version', "Show version") {|val| puts VERSION; exit 0 }
begin
@templates = opts.order!(arguments) do |template|
template_count += 1
options[:tasks] << [:template, template]
end
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument => e
help stdout, opts, e.to_s, ExitStatus::BAD_ARGUMENT
end
options[:tasks] << [:template, '/dev/stdin'] if !options[:explicit] && template_count == 0
options[:trim_level] = tr_level
if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
help stdout, opts
end
end
begin
Rube.generate(stdout, options)
rescue BadArgumentError => e
quit stdout, e.to_s, ExitStatus::BAD_ARGUMENT
rescue MissingRequireError => e
quit stdout, e.to_s, ExitStatus::MISSING_REQUIRE
rescue MissingTemplateError => e
quit stdout, e.to_s, ExitStatus::MISSING_TEMPLATE
rescue ScriptError => e
quit stdout, e.to_s, ExitStatus::SCRIPT_ERROR
end
end
|