@@ -11,13 +11,11 @@ defmodule Kernel.CLI do
11
11
def main ( argv ) do
12
12
argv = for arg <- argv , do: IO . chardata_to_string ( arg )
13
13
14
- { config , argv } = process_argv ( argv , @ blank_config )
14
+ { config , argv } = parse_argv ( argv )
15
15
System . argv ( argv )
16
16
17
17
run fn ->
18
- command_results = Enum . map ( Enum . reverse ( config . commands ) , & process_command ( & 1 , config ) )
19
- command_errors = for { :error , msg } <- command_results , do: msg
20
- errors = Enum . reverse ( config . errors ) ++ command_errors
18
+ errors = process_commands ( config )
21
19
22
20
if errors != [ ] do
23
21
Enum . each ( errors , & IO . puts ( :stderr , & 1 ) )
@@ -55,6 +53,23 @@ defmodule Kernel.CLI do
55
53
end
56
54
end
57
55
56
+ @ doc """
57
+ Parses ARGV returning the CLI config and trailing args.
58
+ """
59
+ def parse_argv ( argv ) do
60
+ parse_argv ( argv , @ blank_config )
61
+ end
62
+
63
+ @ doc """
64
+ Process commands according to the parsed config from `parse_argv/1`.
65
+ Returns all errors.
66
+ """
67
+ def process_commands ( config ) do
68
+ results = Enum . map ( Enum . reverse ( config . commands ) , & process_command ( & 1 , config ) )
69
+ errors = for { :error , msg } <- results , do: msg
70
+ Enum . reverse ( config . errors , errors )
71
+ end
72
+
58
73
## Helpers
59
74
60
75
defp at_exit ( status ) do
@@ -75,7 +90,7 @@ defmodule Kernel.CLI do
75
90
end
76
91
77
92
defp shared_option? ( list , config , callback ) do
78
- case process_shared ( list , config ) do
93
+ case parse_shared ( list , config ) do
79
94
{ [ h | hs ] , _ } when h == hd ( list ) ->
80
95
new_config = % { config | errors: [ "#{ h } : Unknown option" | config . errors ] }
81
96
callback . ( hs , new_config )
@@ -106,77 +121,77 @@ defmodule Kernel.CLI do
106
121
[ ]
107
122
end
108
123
109
- # Process shared options
124
+ # Parse shared options
110
125
111
- defp process_shared ( [ opt | _t ] , _config ) when opt in [ "-v" , "--version" ] do
126
+ defp parse_shared ( [ opt | _t ] , _config ) when opt in [ "-v" , "--version" ] do
112
127
IO . puts "Elixir #{ System . version } "
113
128
System . halt 0
114
129
end
115
130
116
- defp process_shared ( [ "-pa" , h | t ] , config ) do
131
+ defp parse_shared ( [ "-pa" , h | t ] , config ) do
117
132
Enum . each Path . wildcard ( Path . expand ( h ) ) , & Code . prepend_path ( & 1 )
118
- process_shared t , config
133
+ parse_shared t , config
119
134
end
120
135
121
- defp process_shared ( [ "-pz" , h | t ] , config ) do
136
+ defp parse_shared ( [ "-pz" , h | t ] , config ) do
122
137
Enum . each Path . wildcard ( Path . expand ( h ) ) , & Code . append_path ( & 1 )
123
- process_shared t , config
138
+ parse_shared t , config
124
139
end
125
140
126
- defp process_shared ( [ "--app" , h | t ] , config ) do
127
- process_shared t , % { config | commands: & [ { :app , h } | & 1 ] }
141
+ defp parse_shared ( [ "--app" , h | t ] , config ) do
142
+ parse_shared t , % { config | commands: & [ { :app , h } | & 1 ] }
128
143
end
129
144
130
- defp process_shared ( [ "--no-halt" | t ] , config ) do
131
- process_shared t , % { config | halt: false }
145
+ defp parse_shared ( [ "--no-halt" | t ] , config ) do
146
+ parse_shared t , % { config | halt: false }
132
147
end
133
148
134
- defp process_shared ( [ "-e" , h | t ] , config ) do
135
- process_shared t , % { config | commands: [ { :eval , h } | config . commands ] }
149
+ defp parse_shared ( [ "-e" , h | t ] , config ) do
150
+ parse_shared t , % { config | commands: [ { :eval , h } | config . commands ] }
136
151
end
137
152
138
- defp process_shared ( [ "-r" , h | t ] , config ) do
139
- process_shared t , % { config | commands: [ { :require , h } | config . commands ] }
153
+ defp parse_shared ( [ "-r" , h | t ] , config ) do
154
+ parse_shared t , % { config | commands: [ { :require , h } | config . commands ] }
140
155
end
141
156
142
- defp process_shared ( [ "-pr" , h | t ] , config ) do
143
- process_shared t , % { config | commands: [ { :parallel_require , h } | config . commands ] }
157
+ defp parse_shared ( [ "-pr" , h | t ] , config ) do
158
+ parse_shared t , % { config | commands: [ { :parallel_require , h } | config . commands ] }
144
159
end
145
160
146
- defp process_shared ( [ erl , _ | t ] , config ) when erl in [ "--erl" , "--sname" , "--name" , "--cookie" ] do
147
- process_shared t , config
161
+ defp parse_shared ( [ erl , _ | t ] , config ) when erl in [ "--erl" , "--sname" , "--name" , "--cookie" ] do
162
+ parse_shared t , config
148
163
end
149
164
150
- defp process_shared ( [ erl | t ] , config ) when erl in [ "--detached" , "--hidden" , "--gen-debug" ] do
151
- process_shared t , config
165
+ defp parse_shared ( [ erl | t ] , config ) when erl in [ "--detached" , "--hidden" , "--gen-debug" ] do
166
+ parse_shared t , config
152
167
end
153
168
154
- defp process_shared ( list , config ) do
169
+ defp parse_shared ( list , config ) do
155
170
{ list , config }
156
171
end
157
172
158
173
# Process init options
159
174
160
- defp process_argv ( [ "--" | t ] , config ) do
175
+ defp parse_argv ( [ "--" | t ] , config ) do
161
176
{ config , t }
162
177
end
163
178
164
- defp process_argv ( [ "+elixirc" | t ] , config ) do
165
- process_compiler t , config
179
+ defp parse_argv ( [ "+elixirc" | t ] , config ) do
180
+ parse_compiler t , config
166
181
end
167
182
168
- defp process_argv ( [ "+iex" | t ] , config ) do
169
- process_iex t , config
183
+ defp parse_argv ( [ "+iex" | t ] , config ) do
184
+ parse_iex t , config
170
185
end
171
186
172
- defp process_argv ( [ "-S" , h | t ] , config ) do
187
+ defp parse_argv ( [ "-S" , h | t ] , config ) do
173
188
{ % { config | commands: [ { :script , h } | config . commands ] } , t }
174
189
end
175
190
176
- defp process_argv ( [ h | t ] = list , config ) do
191
+ defp parse_argv ( [ h | t ] = list , config ) do
177
192
case h do
178
193
"-" <> _ ->
179
- shared_option? list , config , & process_argv ( & 1 , & 2 )
194
+ shared_option? list , config , & parse_argv ( & 1 , & 2 )
180
195
_ ->
181
196
if Keyword . has_key? ( config . commands , :eval ) do
182
197
{ config , list }
@@ -186,84 +201,84 @@ defmodule Kernel.CLI do
186
201
end
187
202
end
188
203
189
- defp process_argv ( [ ] , config ) do
204
+ defp parse_argv ( [ ] , config ) do
190
205
{ config , [ ] }
191
206
end
192
207
193
- # Process compiler options
208
+ # Parse compiler options
194
209
195
- defp process_compiler ( [ "--" | t ] , config ) do
210
+ defp parse_compiler ( [ "--" | t ] , config ) do
196
211
{ config , t }
197
212
end
198
213
199
- defp process_compiler ( [ "-o" , h | t ] , config ) do
200
- process_compiler t , % { config | output: h }
214
+ defp parse_compiler ( [ "-o" , h | t ] , config ) do
215
+ parse_compiler t , % { config | output: h }
201
216
end
202
217
203
- defp process_compiler ( [ "--no-docs" | t ] , config ) do
204
- process_compiler t , % { config | compiler_options: [ { :docs , false } | config . compiler_options ] }
218
+ defp parse_compiler ( [ "--no-docs" | t ] , config ) do
219
+ parse_compiler t , % { config | compiler_options: [ { :docs , false } | config . compiler_options ] }
205
220
end
206
221
207
- defp process_compiler ( [ "--no-debug-info" | t ] , config ) do
208
- process_compiler t , % { config | compiler_options: [ { :debug_info , false } | config . compiler_options ] }
222
+ defp parse_compiler ( [ "--no-debug-info" | t ] , config ) do
223
+ parse_compiler t , % { config | compiler_options: [ { :debug_info , false } | config . compiler_options ] }
209
224
end
210
225
211
- defp process_compiler ( [ "--ignore-module-conflict" | t ] , config ) do
212
- process_compiler t , % { config | compiler_options: [ { :ignore_module_conflict , true } | config . compiler_options ] }
226
+ defp parse_compiler ( [ "--ignore-module-conflict" | t ] , config ) do
227
+ parse_compiler t , % { config | compiler_options: [ { :ignore_module_conflict , true } | config . compiler_options ] }
213
228
end
214
229
215
- defp process_compiler ( [ "--warnings-as-errors" | t ] , config ) do
216
- process_compiler t , % { config | compiler_options: [ { :warnings_as_errors , true } | config . compiler_options ] }
230
+ defp parse_compiler ( [ "--warnings-as-errors" | t ] , config ) do
231
+ parse_compiler t , % { config | compiler_options: [ { :warnings_as_errors , true } | config . compiler_options ] }
217
232
end
218
233
219
- defp process_compiler ( [ "--verbose" | t ] , config ) do
220
- process_compiler t , % { config | verbose_compile: true }
234
+ defp parse_compiler ( [ "--verbose" | t ] , config ) do
235
+ parse_compiler t , % { config | verbose_compile: true }
221
236
end
222
237
223
- defp process_compiler ( [ h | t ] = list , config ) do
238
+ defp parse_compiler ( [ h | t ] = list , config ) do
224
239
case h do
225
240
"-" <> _ ->
226
- shared_option? list , config , & process_compiler ( & 1 , & 2 )
241
+ shared_option? list , config , & parse_compiler ( & 1 , & 2 )
227
242
_ ->
228
243
pattern = if :filelib . is_dir ( h ) , do: "#{ h } /**/*.ex" , else: h
229
- process_compiler t , % { config | compile: [ pattern | config . compile ] }
244
+ parse_compiler t , % { config | compile: [ pattern | config . compile ] }
230
245
end
231
246
end
232
247
233
- defp process_compiler ( [ ] , config ) do
248
+ defp parse_compiler ( [ ] , config ) do
234
249
{ % { config | commands: [ { :compile , config . compile } | config . commands ] } , [ ] }
235
250
end
236
251
237
- # Process iex options
252
+ # Parse iex options
238
253
239
- defp process_iex ( [ "--" | t ] , config ) do
254
+ defp parse_iex ( [ "--" | t ] , config ) do
240
255
{ config , t }
241
256
end
242
257
243
- # This clause is here so that Kernel.CLI does not error out with "unknown
244
- # option"
245
- defp process_iex ( [ "--dot-iex" , _ | t ] , config ) do
246
- process_iex t , config
258
+ # This clause is here so that Kernel.CLI does not
259
+ # error out with "unknown option"
260
+ defp parse_iex ( [ "--dot-iex" , _ | t ] , config ) do
261
+ parse_iex t , config
247
262
end
248
263
249
- defp process_iex ( [ opt , _ | t ] , config ) when opt in [ "--remsh" ] do
250
- process_iex t , config
264
+ defp parse_iex ( [ opt , _ | t ] , config ) when opt in [ "--remsh" ] do
265
+ parse_iex t , config
251
266
end
252
267
253
- defp process_iex ( [ "-S" , h | t ] , config ) do
268
+ defp parse_iex ( [ "-S" , h | t ] , config ) do
254
269
{ % { config | commands: [ { :script , h } | config . commands ] } , t }
255
270
end
256
271
257
- defp process_iex ( [ h | t ] = list , config ) do
272
+ defp parse_iex ( [ h | t ] = list , config ) do
258
273
case h do
259
274
"-" <> _ ->
260
- shared_option? list , config , & process_iex ( & 1 , & 2 )
275
+ shared_option? list , config , & parse_iex ( & 1 , & 2 )
261
276
_ ->
262
277
{ % { config | commands: [ { :file , h } | config . commands ] } , t }
263
278
end
264
279
end
265
280
266
- defp process_iex ( [ ] , config ) do
281
+ defp parse_iex ( [ ] , config ) do
267
282
{ config , [ ] }
268
283
end
269
284
0 commit comments