@@ -32,7 +32,9 @@ defmodule OptionParser do
32
32
33
33
Note Elixir also converts the switches to underscore atoms, as
34
34
`--source-path` becomes `:source_path`, to better suit Elixir
35
- conventions.
35
+ conventions. This means that option names on the command line cannot contain
36
+ underscores; such options will be reported as `:undefined` (in strict mode)
37
+ or `:invalid` (in basic mode).
36
38
37
39
## Switches
38
40
@@ -224,7 +226,7 @@ defmodule OptionParser do
224
226
else
225
227
{ opt_name , kinds , value } = normalize_option ( tagged , value , switches )
226
228
{ value , kinds , rest } = normalize_value ( value , kinds , rest , strict )
227
- case validate_option ( opt_name , value , kinds ) do
229
+ case validate_option ( value , kinds ) do
228
230
{ :ok , new_value } -> { :ok , opt_name , new_value , rest }
229
231
:invalid -> { :invalid , opt_name_bin , value , rest }
230
232
end
@@ -252,31 +254,31 @@ defmodule OptionParser do
252
254
{ aliases , switches , strict , all }
253
255
end
254
256
255
- defp validate_option ( option , value , kinds ) do
256
- { invalid_opt , value } = cond do
257
+ defp validate_option ( value , kinds ) do
258
+ { is_invalid , value } = cond do
257
259
:invalid in kinds ->
258
- { option , value }
260
+ { true , value }
259
261
:boolean in kinds ->
260
262
case value do
261
263
t when t in [ true , "true" ] -> { nil , true }
262
264
f when f in [ false , "false" ] -> { nil , false }
263
- _ -> { option , value }
265
+ _ -> { true , value }
264
266
end
265
267
:integer in kinds ->
266
268
case Integer . parse ( value ) do
267
269
{ value , "" } -> { nil , value }
268
- _ -> { option , value }
270
+ _ -> { true , value }
269
271
end
270
272
:float in kinds ->
271
273
case Float . parse ( value ) do
272
274
{ value , "" } -> { nil , value }
273
- _ -> { option , value }
275
+ _ -> { true , value }
274
276
end
275
277
true ->
276
278
{ nil , value }
277
279
end
278
280
279
- if invalid_opt do
281
+ if is_invalid do
280
282
:invalid
281
283
else
282
284
{ :ok , value }
@@ -301,11 +303,11 @@ defmodule OptionParser do
301
303
if alias = aliases [ opt ] do
302
304
{ :default , alias }
303
305
else
304
- { :unknown , opt }
306
+ :unknown
305
307
end
306
308
end
307
309
308
- defp option_defined? ( { :unknown , _option } , _switches ) do
310
+ defp option_defined? ( :unknown , _switches ) do
309
311
false
310
312
end
311
313
@@ -317,8 +319,8 @@ defmodule OptionParser do
317
319
Keyword . has_key? ( switches , option )
318
320
end
319
321
320
- defp normalize_option ( { :unknown , option } , value , _switches ) do
321
- { option , [ :invalid ] , value }
322
+ defp normalize_option ( :unknown , value , _switches ) do
323
+ { nil , [ :invalid ] , value }
322
324
end
323
325
324
326
defp normalize_option ( { :negated , option } , nil , switches ) do
@@ -374,29 +376,46 @@ defmodule OptionParser do
374
376
end
375
377
end
376
378
377
- defp to_underscore ( option ) do
378
- for << c <- option >> , into: "" , do: << if ( c == ?- , do: ?_ , else: c ) >>
379
- end
379
+ defp to_underscore ( option ) , do: to_underscore ( option , << >> )
380
+
381
+ defp to_underscore ( "_" <> _rest , _acc ) , do: nil
382
+
383
+ defp to_underscore ( "-" <> rest , acc ) ,
384
+ do: to_underscore ( rest , acc <> "_" )
385
+
386
+ defp to_underscore ( << c >> <> rest , acc ) ,
387
+ do: to_underscore ( rest , << acc :: binary , c >> )
388
+
389
+ defp to_underscore ( << >> , acc ) , do: acc
380
390
381
391
defp get_option ( option ) do
382
- option |> to_underscore |> String . to_atom
392
+ if str = to_underscore ( option ) do
393
+ String . to_atom ( str )
394
+ end
383
395
end
384
396
385
397
defp reverse_negated ( negated ) do
386
398
String . to_atom ( "no_" <> Atom . to_string ( negated ) )
387
399
end
388
400
389
401
defp get_negated ( "no-" <> rest = option , value , switches ) do
390
- negated = get_option ( rest )
391
- option = if Keyword . has_key? ( switches , negated ) and value == nil do
392
- negated
402
+ if negated = get_option ( rest ) do
403
+ option = if Keyword . has_key? ( switches , negated ) and value == nil do
404
+ negated
405
+ else
406
+ get_option ( option )
407
+ end
408
+ { :negated , option }
393
409
else
394
- get_option ( option )
410
+ :unknown
395
411
end
396
- { :negated , option }
397
412
end
398
413
399
414
defp get_negated ( rest , _value , _switches ) do
400
- { :default , get_option ( rest ) }
415
+ if option = get_option ( rest ) do
416
+ { :default , option }
417
+ else
418
+ :unknown
419
+ end
401
420
end
402
421
end
0 commit comments