1- defrecord Kernel.CLI.Config , commands: [ ] , output: "." ,
2- compile: [ ] , halt: true , compiler_options: [ ]
3-
41defmodule Kernel.CLI do
5- @ moduledoc false
6-
7- @ doc """
8- Invoked directly from erlang boot process. It parses all argv
9- options and execute them in the order they are specified.
2+ @ moduledoc """
3+ Module responsible for controlling Elixir's CLI
104 """
11- def process_argv ( options ) do
12- { config , argv } = process_options ( options , Kernel.CLI.Config . new )
5+
6+ defrecord Config , commands: [ ] , output: "." ,
7+ compile: [ ] , halt: true , compiler_options: [ ]
8+
9+ # This is the API invoked by Elixir boot process.
10+ @ doc false
11+ def main ( options ) do
12+ { config , argv } = process_argv ( options , Kernel.CLI.Config . new )
1313
1414 argv = lc arg inlist argv , do: list_to_binary ( arg )
1515 :gen_server . call ( :elixir_code_server , { :argv , argv } )
1616
17- all_commands = Enum . reverse ( config . commands )
17+ run fn ->
18+ Enum . map Enum . reverse ( config . commands ) , process_command ( & 1 , config )
19+ :gen_server . cast ( :elixir_code_server , :finished )
20+ end , config . halt
21+ end
1822
19- run fn -> Enum . map all_commands , process_command ( & 1 , config ) end , config . halt
23+ @ doc """
24+ Wait until the CLI finishes procesing options.
25+ """
26+ def wait_until_finished do
27+ case :gen_server . call ( :elixir_code_server , { :wait_until_finished , self } ) do
28+ :wait ->
29+ receive do
30+ { :elixir_code_server , :finished } -> :ok
31+ end
32+ :ok -> :ok
33+ end
2034 end
2135
2236 @ doc """
23- Runs the given function and wraps any exception or
24- error in messages useful for the command line.
37+ Runs the given function by catching any failure
38+ and printing them to stdout. `at_exit` hooks are
39+ also invoked before exiting.
40+
41+ This function is used by Elixir's CLI and also
42+ by escripts generated by Elixir.
2543 """
2644 def run ( fun , halt // true ) do
2745 try do
@@ -141,15 +159,15 @@ defmodule Kernel.CLI do
141159
142160 # Process init options
143161
144- def process_options ( [ '--' | t ] , config ) do
162+ defp process_argv ( [ '--' | t ] , config ) do
145163 { config , t }
146164 end
147165
148- def process_options ( [ '--compile' | t ] , config ) do
166+ defp process_argv ( [ '--compile' | t ] , config ) do
149167 process_compiler t , config
150168 end
151169
152- def process_options ( [ '-S' , h | t ] , config ) do
170+ defp process_argv ( [ '-S' , h | t ] , config ) do
153171 exec = System . find_executable ( h )
154172 if exec do
155173 { config . prepend_commands ( [ require: list_to_binary ( exec ) ] ) , t }
@@ -159,17 +177,17 @@ defmodule Kernel.CLI do
159177 end
160178 end
161179
162- def process_options ( [ h | t ] = list , config ) do
180+ defp process_argv ( [ h | t ] = list , config ) do
163181 case h do
164182 '-' ++ _ ->
165- shared_option? list , config , process_options ( & 1 , & 2 )
183+ shared_option? list , config , process_argv ( & 1 , & 2 )
166184 _ ->
167185 h = list_to_binary ( h )
168186 { config . prepend_commands ( [ require: h ] ) , t }
169187 end
170188 end
171189
172- def process_options ( [ ] , config ) do
190+ defp process_argv ( [ ] , config ) do
173191 { config , [ ] }
174192 end
175193
0 commit comments