@@ -3,41 +3,33 @@ defmodule InterpolationApp.CLI.ConfigParser do
33 Parses args for interpolation-app CLI
44 """
55
6- @ methods [ "linear" , "lagrange2 " , "lagrange3 " , "newton" ]
6+ @ methods [ "linear" , "lagrange3 " , "lagrange4 " , "newton" ]
77
88 def parse_args ( args ) do
99 args
1010 |> Enum . reduce ( % { } , & parse_arg / 2 )
1111 |> validate_args ( )
1212 end
1313
14- defp parse_arg ( "--method1 =" <> method , acc ) do
15- Map . put ( acc , :method1 , String . trim ( method ) )
14+ defp parse_arg ( "methods =" <> method , acc ) do
15+ Map . put ( acc , :methods , String . split ( String . trim ( method ) , "," ) )
1616 end
1717
18- defp parse_arg ( "--method2=" <> method , acc ) do
19- Map . put ( acc , :method2 , String . trim ( method ) )
20- end
21-
22- defp parse_arg ( "--step=" <> step , acc ) do
18+ defp parse_arg ( "step=" <> step , acc ) do
2319 Map . put ( acc , :step , String . trim ( step ) )
2420 end
2521
26- defp parse_arg ( "-- accuracy=" <> accuracy , acc ) do
22+ defp parse_arg ( "accuracy=" <> accuracy , acc ) do
2723 Map . put ( acc , :accuracy , String . trim ( accuracy ) )
2824 end
2925
3026 defp parse_arg ( _ , acc ) , do: acc
3127
32- defp validate_args ( % { method1: method1 , method2: method2 , step: step , accuracy: accuracy } ) do
28+ defp validate_args ( % { methods: methods , step: step , accuracy: accuracy } ) do
3329 cond do
34- ! valid_method? ( method1 ) ->
30+ ! valid_methods? ( methods ) ->
3531 { :error ,
36- "Неправильный method1: #{ method1 } . Должен быть один из [#{ Enum . join ( @ methods , ", " ) } ]." }
37-
38- ! valid_method? ( method2 ) ->
39- { :error ,
40- "Неправильный method2: #{ method2 } . Должен быть один из [#{ Enum . join ( @ methods , ", " ) } ]." }
32+ "Неправильный ввод методов: Методы должны быть перечислены через запятую и не повторяться.\n Метод должен быть один из [#{ Enum . join ( @ methods , ", " ) } ]." }
4133
4234 ! valid_step? ( step ) ->
4335 { :error ,
@@ -49,23 +41,24 @@ defmodule InterpolationApp.CLI.ConfigParser do
4941 true ->
5042 { :ok ,
5143 % {
52- method1: module_from_method ( method1 ) ,
53- method2: module_from_method ( method2 ) ,
44+ methods: Enum . map ( methods , & module_from_method / 1 ) ,
5445 step: elem ( Float . parse ( step ) , 0 ) ,
5546 accuracy: elem ( Integer . parse ( accuracy ) , 0 )
5647 } }
5748 end
5849 end
5950
60- defp validate_args ( % { method1: method1 , method2: method2 , step: step } ) do
61- validate_args ( % { method1: method1 , method2: method2 , step: step , accuracy: "3" } )
51+ defp validate_args ( % { methods: methods , step: step } ) do
52+ validate_args ( % { methods: methods , step: step , accuracy: "3" } )
6253 end
6354
6455 defp validate_args ( _ ) do
65- { :error , "Ошибка! Обязательные аргументы: --method1=, --method2 =, --step=" }
56+ { :error , "Ошибка! Обязательные аргументы: --methods =, --step=" }
6657 end
6758
68- defp valid_method? ( method ) , do: method in @ methods
59+ defp valid_methods? ( methods ) do
60+ Enum . uniq ( methods ) == methods and Enum . all? ( methods , fn method -> method in @ methods end )
61+ end
6962
7063 defp valid_step? ( step ) do
7164 case Float . parse ( step ) do
0 commit comments