Skip to content

Commit dd43e79

Browse files
author
Jonathan J Hunt
committed
Added -v --version option to all elixir commands.
Fixes #1178
1 parent e93d419 commit dd43e79

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lib/elixir/lib/kernel/cli.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ defmodule Kernel.CLI do
102102

103103
# Process shared options
104104

105-
defp process_shared(["-v"|t], config) do
105+
defp process_shared([opt|t], config) when opt in ["-v", "--version"] do
106106
IO.puts "Elixir #{System.version}"
107-
process_shared t, config
107+
System.halt 0
108108
end
109109

110110
defp process_shared(["-pa",h|t], config) do

lib/mix/lib/mix/cli.ex

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ defmodule Mix.CLI do
55
Runs Mix according to the command line arguments.
66
"""
77
def run(args // System.argv) do
8-
if help?(args) do
9-
display_banner()
10-
else
11-
proceed(args)
8+
case check_for_shortcuts(args) do
9+
:help ->
10+
display_banner()
11+
:version ->
12+
display_version()
13+
nil ->
14+
proceed(args)
1215
end
1316
end
1417

@@ -68,6 +71,16 @@ defmodule Mix.CLI do
6871
run_task "help", []
6972
end
7073

71-
defp help?([first_arg|_]) when first_arg in ["--help", "-h", "-help"], do: true
72-
defp help?(_), do: false
74+
defp display_version() do
75+
IO.puts "Elixir #{System.version}"
76+
end
77+
78+
# Check for --help or --version in the args
79+
defp check_for_shortcuts([first_arg|_]) when first_arg in
80+
["--help", "-h", "-help"], do: :help
81+
82+
defp check_for_shortcuts([first_arg|_]) when first_arg in
83+
["--version", "-v"], do: :version
84+
85+
defp check_for_shortcuts(_), do: nil
7386
end

lib/mix/test/mix/cli_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ defmodule Mix.CLITest do
5858
end
5959
end
6060

61+
test "--help smoke test" do
62+
in_fixture "only_mixfile", fn ->
63+
output = mix "--help"
64+
assert output =~ %r"mix compile\s+# Compile source files"
65+
refute output =~ %r"mix invalid"
66+
end
67+
end
68+
69+
test "--version smoke test" do
70+
in_fixture "only_mixfile", fn ->
71+
output = mix "--version"
72+
assert output =~ %r"Elixir [0-9\.a-z]+"
73+
refute output =~ %r"Something silly"
74+
end
75+
end
76+
6177
test "help TASK smoke test" do
6278
in_fixture "only_mixfile", fn ->
6379
output = mix "help compile"

0 commit comments

Comments
 (0)