Skip to content

Commit e7fcaff

Browse files
author
José Valim
committed
Merge pull request #2289 from lexmag/master
Change `OptionParser` to remove `--`. Fix command-line arguments handling.
2 parents a4b01b2 + ee561bd commit e7fcaff

File tree

9 files changed

+69
-16
lines changed

9 files changed

+69
-16
lines changed

lib/elixir/lib/code.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ defmodule Code do
463463
if File.regular?(file) do
464464
file
465465
else
466-
raise LoadError, file: file
466+
raise Code.LoadError, file: file
467467
end
468468
end
469469
end

lib/elixir/lib/kernel/cli.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ defmodule Kernel.CLI do
178178
"-" <> _ ->
179179
shared_option? list, config, &process_argv(&1, &2)
180180
_ ->
181-
{%{config | commands: [{:file, h} | config.commands]}, t}
181+
if Keyword.has_key?(config.commands, :eval) do
182+
{config, list}
183+
else
184+
{%{config | commands: [{:file, h} | config.commands]}, t}
185+
end
182186
end
183187
end
184188

lib/elixir/lib/option_parser.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ defmodule OptionParser do
120120
parse(argv, aliases, switches, [], [], [], all)
121121
end
122122

123-
defp parse(["--"|_] = value, _aliases, _switches, dict, args, invalid, _all) do
124-
{Enum.reverse(dict), Enum.reverse(args, value), Enum.reverse(invalid)}
123+
defp parse(["--"|t], _aliases, _switches, dict, args, invalid, _all) do
124+
{Enum.reverse(dict), Enum.reverse(args, t), Enum.reverse(invalid)}
125125
end
126126

127127
defp parse(["-" <> option|t], aliases, switches, dict, args, invalid, all) do

lib/elixir/test/elixir/code_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ defmodule CodeTest do
7070

7171
test :eval_file do
7272
assert Code.eval_file(fixture_path("code_sample.exs")) == {3, [var: 3]}
73+
74+
assert_raise Code.LoadError, fn ->
75+
Code.eval_file("non_existent.exs")
76+
end
7377
end
7478

7579
test :require do

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ defmodule Kernel.CLI.InitTest do
77

88
test "handles code on initialization" do
99
assert elixir('-e "IO.puts [?3]"') == '3\n'
10+
assert elixir('#{fixture_path("init_sample.exs")}') == '3\n'
11+
end
12+
13+
test "argv handling" do
14+
expected = '#{inspect ["sample.exs", "-o", "1", "2"]}\n'
15+
assert elixir('-e "IO.puts inspect(System.argv)" sample.exs -o 1 2') == expected
16+
assert elixir('-e "IO.puts inspect(System.argv)" -- sample.exs -o 1 2') == expected
17+
assert elixir('-e "IO.puts inspect(System.argv)" --hidden sample.exs -o 1 2') == expected
1018

11-
result = elixir('-e "IO.puts inspect(System.argv)" #{fixture_path("init_sample.exs")} -o 1 2 3')
12-
assert result == '#{inspect ["-o", "1", "2", "3"]}\n3\n'
19+
result = elixir('-e "IO.puts inspect(System.argv)" -- --hidden sample.exs -o 1 2')
20+
assert result == '#{inspect ["--hidden", "sample.exs", "-o", "1", "2"]}\n'
1321
end
1422
end
1523

lib/elixir/test/elixir/option_parser_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,15 @@ defmodule OptionParserTest do
154154

155155
test "stops on --" do
156156
options = OptionParser.parse(["--source", "from_docs/", "--", "1", "2", "3"])
157-
assert options == {[source: "from_docs/"], ["--", "1", "2", "3"], []}
157+
assert options == {[source: "from_docs/"], ["1", "2", "3"], []}
158158

159159
options = OptionParser.parse_head(["--source", "from_docs/", "--", "1", "2", "3"])
160-
assert options == {[source: "from_docs/"], ["--", "1", "2", "3"], []}
160+
assert options == {[source: "from_docs/"], ["1", "2", "3"], []}
161161

162162
options = OptionParser.parse(["--no-dash", "foo", "bar", "--", "-x"])
163+
assert options == {[no_dash: true], ["foo", "bar", "-x"], []}
164+
165+
options = OptionParser.parse_head(["--no-dash", "foo", "bar", "--", "-x"])
163166
assert options == {[no_dash: true], ["foo", "bar", "--", "-x"], []}
164167
end
165168

lib/mix/lib/mix/scm/git.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Mix.SCM.Git do
1717
case Enum.find_value [:branch, :ref, :tag], &List.keyfind(lock_opts, &1, 0) do
1818
{:ref, _} -> lock <> " (ref)"
1919
{key, val} -> lock <> " (#{key}: #{val})"
20-
nil -> lock
20+
nil -> lock
2121
end
2222
_ ->
2323
nil

lib/mix/lib/mix/tasks/run.ex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ defmodule Mix.Tasks.Run do
3434
def run(args) do
3535
{opts, head, _} = OptionParser.parse_head(args,
3636
aliases: [r: :require, pr: :parallel_require, e: :eval],
37-
switches: [parallel_require: :keep, require: :keep])
37+
switches: [parallel_require: :keep, require: :keep, eval: :keep])
3838

3939
# Require the project to be available
4040
Mix.Project.get!
4141

42-
file =
43-
case head do
44-
["--"|t] -> System.argv(t); nil
45-
[h|t] -> System.argv(t); h
46-
[] -> System.argv([]); nil
42+
{file, argv} =
43+
case {Keyword.has_key?(opts, :eval), head} do
44+
{true, _} -> {nil, head}
45+
{_, [h|t]} -> {h, t}
46+
{_, []} -> {nil, []}
4747
end
4848

49+
System.argv(argv)
50+
4951
# Start app after rewriting System.argv,
5052
# but before requiring and evaling
5153
Mix.Task.run "app.start", args

lib/mix/test/mix/tasks/run_test.exs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ defmodule Mix.Tasks.RunTest do
1313
end
1414
end
1515

16-
test "run requires files before evaling commands" do
16+
setup do
1717
Mix.Project.push MixTest.Case.Sample
18+
end
19+
20+
test "run requires files before evaling commands" do
1821
git_repo = fixture_path("git_repo/lib/git_repo.ex")
1922

2023
in_fixture "no_mixfile", fn ->
@@ -27,4 +30,33 @@ defmodule Mix.Tasks.RunTest do
2730
after
2831
purge [GitRepo]
2932
end
33+
34+
test "run rewrites System.argv" do
35+
in_fixture "no_mixfile", fn ->
36+
File.write! file = "argv.exs", "send self, {:system_argv, System.argv}"
37+
unload_file = fn ->
38+
Code.unload_files [Path.expand(file)]
39+
end
40+
41+
Mix.Tasks.Run.run [file]
42+
assert_received {:system_argv, []}
43+
44+
unload_file.()
45+
Mix.Tasks.Run.run [file, "foo", "-e", "bar"]
46+
assert_received {:system_argv, ["foo", "-e", "bar"]}
47+
48+
unload_file.()
49+
Mix.Tasks.Run.run ["-e", "send self, {:system_argv, System.argv}", file, "foo", "-x", "bar"]
50+
assert_received {:system_argv, [file, "foo", "-x", "bar"]}
51+
52+
unload_file.()
53+
Mix.Tasks.Run.run [
54+
"-e", "send self, :evaled",
55+
"-e", "send self, {:system_argv, System.argv}",
56+
"--no-compile", file, "-x", "bar"
57+
]
58+
assert_received :evaled
59+
assert_received {:system_argv, [file, "-x", "bar"]}
60+
end
61+
end
3062
end

0 commit comments

Comments
 (0)