Skip to content

Commit ae0a6ce

Browse files
committed
Improve flag error messages in CLI
Now consistent in error messages and tries to show as many error messages as possible before exiting.
1 parent e9a91d1 commit ae0a6ce

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

lib/elixir/lib/kernel/cli.ex

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ defmodule Kernel.CLI do
33
Module responsible for controlling Elixir's CLI
44
"""
55

6-
defrecord Config, commands: [], output: ".",
7-
compile: [], halt: true, compiler_options: []
6+
defrecord Config, commands: [], output: ".", compile: [],
7+
halt: true, compiler_options: [], errors: []
88

99
# This is the API invoked by Elixir boot process.
1010
@doc false
@@ -15,7 +15,13 @@ defmodule Kernel.CLI do
1515
:elixir_code_server.cast({ :argv, argv })
1616

1717
run fn ->
18-
Enum.map Enum.reverse(config.commands), process_command(&1, config)
18+
command_results = Enum.map(Enum.reverse(config.commands), process_command(&1, config))
19+
command_errors = Enum.filter_map(command_results, &1 != :ok, fn {:error, msg} -> msg end)
20+
errors = Enum.reverse(config.errors) ++ command_errors
21+
if errors != [] do
22+
Enum.each(errors, fn msg -> IO.puts(:stderr, msg) end)
23+
System.halt(1)
24+
end
1925
end, config.halt
2026
end
2127

@@ -83,15 +89,11 @@ defmodule Kernel.CLI do
8389
unless hooks == [], do: at_exit(status)
8490
end
8591

86-
defp invalid_option(option) do
87-
IO.puts(:stderr, "Unknown option #{option}")
88-
System.halt(1)
89-
end
90-
9192
defp shared_option?(list, config, callback) do
9293
case process_shared(list, config) do
93-
{ [h|_], _ } when h == hd(list) ->
94-
invalid_option h
94+
{ [h|hs], _ } when h == hd(list) ->
95+
new_config = config.update_errors ["Unknown option #{h}" | &1]
96+
callback.(hs, new_config)
9597
{ new_list, new_config } ->
9698
callback.(new_list, new_config)
9799
end
@@ -129,8 +131,7 @@ defmodule Kernel.CLI do
129131
defp process_shared(["-r",h|t], config) do
130132
files = Path.wildcard(h)
131133
if files == [] do
132-
IO.puts(:stderr, "-r : No files matched pattern #{h}")
133-
System.halt(1)
134+
process_shared t, config.update_errors ["-r : No files matched pattern #{h}" | &1]
134135
else
135136
process_shared t, Enum.reduce(files, config, fn path, config ->
136137
config.update_commands [{:require,path}|&1]
@@ -165,8 +166,7 @@ defmodule Kernel.CLI do
165166
if exec do
166167
{ config.update_commands([{:require,exec}|&1]), t }
167168
else
168-
IO.puts(:stderr, "Could not find executable #{h}")
169-
System.halt(1)
169+
{ config.update_errors(["-S : Could not find executable #{h}" | &1]), t }
170170
end
171171
end
172172

@@ -223,32 +223,37 @@ defmodule Kernel.CLI do
223223

224224
defp process_command({:eval, expr}, _config) when is_binary(expr) do
225225
Code.eval(expr, [])
226+
:ok
226227
end
227228

228229
defp process_command({:app, app}, _config) when is_binary(app) do
229230
case Application.Behaviour.start(binary_to_atom(app)) do
230231
{ :error, reason } ->
231-
IO.puts(:stderr, "Could not start application #{app}: #{inspect reason}")
232-
System.halt(1)
232+
{ :error, "--app : Could not start application #{app}: #{inspect reason}" }
233233
:ok ->
234234
:ok
235235
end
236236
end
237237

238238
defp process_command({:require, file}, _config) when is_binary(file) do
239-
Code.require_file(file)
239+
if File.regular?(file) do
240+
Code.require_file(file)
241+
:ok
242+
else
243+
{ :error, "-r : No file named #{file}" }
244+
end
240245
end
241246

242247
defp process_command({:parallel_require, pattern}, _config) when is_binary(pattern) do
243248
files = Path.wildcard(pattern)
244249
files = Enum.uniq(files)
245250
files = Enum.filter files, File.regular?(&1)
246251

247-
if files == [] do
248-
IO.puts(:stderr, "-pr : No files matched pattern #{pattern}")
249-
System.halt(1)
250-
else
252+
if files != [] do
251253
Kernel.ParallelRequire.files(files)
254+
:ok
255+
else
256+
{ :error, "-pr : No files matched pattern #{pattern}" }
252257
end
253258
end
254259

@@ -259,8 +264,13 @@ defmodule Kernel.CLI do
259264
files = Enum.uniq(List.concat(files))
260265
files = Enum.filter files, File.regular?(&1)
261266

262-
Code.compiler_options(config.compiler_options)
263-
Kernel.ParallelCompiler.files_to_path(files, config.output,
264-
fn file -> IO.puts "Compiled #{file}" end)
267+
if files != [] do
268+
Code.compiler_options(config.compiler_options)
269+
Kernel.ParallelCompiler.files_to_path(files, config.output,
270+
fn file -> IO.puts "Compiled #{file}" end)
271+
:ok
272+
else
273+
{ :error, "--compile : No files matched patterns #{patterns}" }
274+
end
265275
end
266276
end

0 commit comments

Comments
 (0)