-
Notifications
You must be signed in to change notification settings - Fork 414
/
options.ex
137 lines (109 loc) · 4.12 KB
/
options.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
defmodule Credo.CLI.Options do
@moduledoc """
The `Options` struct represents the options given on the command line.
The `Options` struct is stored as part of the `Execution` struct.
"""
alias Credo.Priority
defstruct command: nil,
path: nil,
args: [],
switches: nil,
unknown_switches: [],
unknown_args: []
@doc """
Returns a `Options` struct for the given parameters.
iex> Credo.CLI.Options.parse(["alice", "--debug"], ".", ["alice", "bob", "eve"], [], [debug: :boolean], [])
%Credo.CLI.Options{args: [], command: "alice", path: ".", switches: %{debug: true}, unknown_args: [], unknown_switches: []}
iex> Credo.CLI.Options.parse(["alice", "--friend", "bob", "--friend", "eve"], ".", ["alice", "bob", "eve"], [], [friend: :keep], [])
%Credo.CLI.Options{args: [], command: "alice", path: ".", switches: %{friend: ["bob", "eve"]}, unknown_args: [], unknown_switches: []}
iex> Credo.CLI.Options.parse(["alice", "--friend", "bob", "--friend", "eve"], ".", ["alice", "bob", "eve"], [], [friend: [:string, :keep]], [])
%Credo.CLI.Options{args: [], command: "alice", path: ".", switches: %{friend: ["bob", "eve"]}, unknown_args: [], unknown_switches: []}
"""
def parse(argv, current_dir, command_names, ignored_args, switches_definition, aliases) do
argv
|> OptionParser.parse(strict: switches_definition, aliases: aliases)
|> parse_result(current_dir, command_names, ignored_args, switches_definition)
end
defp parse_result(
{switches_keywords, args, unknown_switches_keywords},
current_dir,
command_names,
ignored_args,
switches_definition
) do
args = Enum.reject(args, &Enum.member?(ignored_args, &1))
{command, path, unknown_args} = split_args(args, current_dir, command_names)
{switches_keywords, extra_unknown_switches} = patch_switches(switches_keywords)
switch_definitions_for_lists =
Enum.filter(switches_definition, fn
{_name, :keep} -> true
{_name, [_, :keep]} -> true
{_name, _value} -> false
end)
switches_with_lists_as_map =
switch_definitions_for_lists
|> Enum.map(fn {name, _value} ->
{name, Keyword.get_values(switches_keywords, name)}
end)
|> Enum.into(%{})
switches =
switches_keywords
|> Enum.into(%{})
|> Map.merge(switches_with_lists_as_map)
%__MODULE__{
command: command,
path: path,
args: unknown_args,
switches: switches,
unknown_switches: unknown_switches_keywords ++ extra_unknown_switches
}
end
defp patch_switches(switches_keywords) do
{switches, unknowns} = Enum.map_reduce(switches_keywords, [], &patch_switch/2)
switches = Enum.reject(switches, &(&1 == nil))
{switches, unknowns}
end
defp patch_switch({:min_priority, str}, unknowns) do
priority = priority_as_name(str) || priority_as_number(str)
case priority do
nil -> {nil, [{"--min-priority", str} | unknowns]}
int -> {{:min_priority, int}, unknowns}
end
end
defp patch_switch(switch, unknowns), do: {switch, unknowns}
defp priority_as_name(str), do: Priority.to_integer(str)
defp priority_as_number(str) do
case Integer.parse(str) do
{int, ""} -> int
_ -> nil
end
end
defp split_args([], current_dir, _) do
{path, unknown_args} = extract_path([], current_dir)
{nil, path, unknown_args}
end
defp split_args([head | tail] = args, current_dir, command_names) do
if Enum.member?(command_names, head) do
{path, unknown_args} = extract_path(tail, current_dir)
{head, path, unknown_args}
else
{path, unknown_args} = extract_path(args, current_dir)
{nil, path, unknown_args}
end
end
defp extract_path([], base_dir) do
{base_dir, []}
end
defp extract_path([path | tail] = args, base_dir) do
if File.exists?(path) or path =~ ~r/[\?\*]/ do
{path, tail}
else
path_with_base = Path.join(base_dir, path)
if File.exists?(path_with_base) do
{path_with_base, tail}
else
{base_dir, args}
end
end
end
end