Class: Opal::CLIOptions

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/opal/cli_options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLIOptions

Returns a new instance of CLIOptions.



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
# File 'lib/opal/cli_options.rb', line 8

def initialize
  super
  @options = {}

  self.banner = 'Usage: opal [options] -- [programfile]'

  separator ''

  on('--repl', 'Run the Opal REPL') do
    options[:repl] = true
  end

  on('-v', '--verbose', 'print version number, then turn on verbose mode') do
    print_version
    exit if ARGV.empty?
    options[:verbose] = true
  end

  on('--verbose', 'turn on verbose mode (set $VERBOSE to true)') do
    options[:verbose] = true
  end

  on('-d', '--debug', 'turn on debug mode (set $DEBUG to true)') do
    options[:debug] = true
  end

  on('--version', 'Print the version') do
    print_version
    exit
  end

  on('-h', '--help', 'Show this message') do
    puts self
    exit
  end

  section 'Basic Options:'

  on('-e', '--eval SOURCE', String,
    'One line of script. Several -e\'s allowed. Omit [programfile]'
  ) do |source|
    options[:evals] ||= []
    options[:evals] << source
  end

  on('-r', '--require LIBRARY', String,
    'Require the library before executing your script'
  ) do |library|
    options[:requires] ||= []
    options[:requires] << library
  end

  on('-q', '--rbrequire LIBRARY', String,
    'Require the library in Ruby context before compiling'
  ) do |library|
    options[:rbrequires] ||= []
    options[:rbrequires] << library
  end

  on('-I', '--include DIR', 'Append a load path (may be used more than once)') do |i|
    options[:load_paths] ||= []
    options[:load_paths] << i
  end

  on('-s', '--stub FILE', String, 'Stubbed files will be compiled as empty files') do |stub|
    options[:stubs] ||= []
    options[:stubs] << stub
  end

  on('-p', '--preload FILE', String, 'Preloaded files will be prepared for dynamic requires') do |stub|
    options[:preload] ||= []
    options[:preload] << stub
  end

  on('-g', '--gem GEM_NAME', String, 'Adds the specified GEM_NAME to Opal\'s load path.') do |g|
    options[:gems] ||= []
    options[:gems] << g
  end

  section 'Running Options:'

  on('-R', '--runner RUNNER', Opal::CliRunners.to_h.keys, 'Choose the runner:', "nodejs (default), #{(Opal::CliRunners.to_h.keys - %i[nodejs compiler]).join(', ')}") do |runner|
    options[:runner] = runner.to_sym
  end

  on('--server-port PORT', 'Set the port for the server runner (default port: 3000)') do |port|
    options[:runner_options] ||= {}
    options[:runner_options][:port] = port.to_i
  end

  on('--runner-options JSON', 'Set options specific to the selected runner as a JSON string (e.g. port for server)') do |json_options|
    require 'json'
    runner_options = JSON.parse(json_options, symbolize_names: true)
    options[:runner_options] ||= {}
    options[:runner_options].merge!(runner_options)
  end

  section 'Builder Options:'

  on('-c', '--compile', 'Compile to JavaScript') do
    options[:runner] = :compiler
  end

  on('-o', '--output FILE', 'Output JavaScript to FILE') do |file|
    options[:output] = File.open(file, 'w')
  end

  on('-P', '--map FILE', 'Output source map to FILE') do |file|
    options[:runner_options] ||= {}
    options[:runner_options][:map_file] = file
  end

  on('--no-source-map', "Don't append source map to a compiled file") do
    options[:runner_options] ||= {}
    options[:runner_options][:no_source_map] = true
  end

  on('--watch', 'Run the compiler in foreground, recompiling every filesystem change') do
    options[:runner_options] ||= {}
    options[:runner_options][:watch] = true
  end

  on('--no-cache', 'Disable filesystem cache') do
    options[:no_cache] = true
  end

  on('-L', '--library', 'Compile only required libraries. Omit [programfile] and [-e]. Assumed [-cOE].') do
    options[:lib_only] = true
    options[:no_exit] = true
    options[:compile] = true
    options[:skip_opal_require] = true
  end

  on('-O', '--no-opal', 'Disable implicit `require "opal"`') do
    options[:skip_opal_require] = true
  end

  on('-E', '--no-exit', 'Do not append a Kernel#exit at the end of file') do
    options[:no_exit] = true
  end

  section 'Compiler Options:'

  on('--use-strict', 'Enables JavaScript\'s strict mode (i.e., adds \'use strict\'; statement)') do
    options[:use_strict] = true
  end

  on('--esm', 'Wraps compiled bundle as for ES6 module') do
    options[:esm] = true
  end

  on('-A', '--arity-check', 'Enable arity check') do
    options[:arity_check] = true
  end

  dynamic_require_levels = %w[error warning ignore]
  on('-D', '--dynamic-require LEVEL', dynamic_require_levels,
    'Set level of dynamic require severity.',
    "(default: error, values: #{dynamic_require_levels.join(', ')})"
  ) do |level|
    options[:dynamic_require_severity] = level.to_sym
  end

  missing_require_levels = %w[error warning ignore]
  on('--missing-require LEVEL', missing_require_levels,
    'Set level of missing require severity.',
    "(default: error, values: #{missing_require_levels.join(', ')})"
  ) do |level|
    options[:missing_require_severity] = level.to_sym
  end

  on('--enable-source-location', 'Compiles source location for each method definition.') do
    options[:enable_source_location] = true
  end

  on('--parse-comments', 'Compiles comments for each method definition.') do
    options[:parse_comments] = true
  end

  on('--enable-file-source-embed', 'Embeds file sources to be accessed by applications.') do
    options[:enable_file_source_embed] = true
  end

  on('--irb', 'Enable IRB var mode') do
    options[:irb] = true
  end

  on('-M', '--no-method-missing', 'Disable method missing') do
    options[:method_missing] = false
  end

  on('-F', '--file FILE', 'Set filename for compiled code') do |file|
    options[:file] = file
  end

  on('-V', '(deprecated; always enabled) Enable inline Operators') do
    warn '* -V is deprecated and has no effect'
    options[:inline_operators] = true
  end

  section 'Debug Options:'

  on('--sexp', 'Show Sexps') do
    options[:sexp] = true
  end

  on('--debug-source-map', 'Debug source map') do
    options[:debug_source_map] = true
  end

  separator ''
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



221
222
223
# File 'lib/opal/cli_options.rb', line 221

def options
  @options
end