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