Skip to content

Commit d0d3033

Browse files
committed
Used case-of pattern matching to improve safety.
Improved debug print statements.
1 parent 7b37fe5 commit d0d3033

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/Diffr/Diff/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
module Main ( main ) where
2222

23+
import Data.Char( intToDigit )
2324
import Diffr.Util.ArgumentsProcessor( containsHelpArgument, extractOutputFile )
2425
import System.Environment( getArgs )
2526
import System.Exit ( exitFailure, exitSuccess )
@@ -33,7 +34,7 @@ main = do
3334
printUsage
3435
exitFailure
3536
else do
36-
print ( length args )
37+
print ( [intToDigit ( length args )] ++ " arguments" )
3738
print ( extractOutputFile args )
3839
exitSuccess
3940

src/Diffr/Util/ArgumentsProcessor.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ import Data.Char ( toLower )
2424

2525
-- | 'containsHelpArgument' checks if the given list of strings contains the help argument.
2626
containsHelpArgument :: [[Char]] -> Bool
27-
containsHelpArgument args = any isHelpArgument args
27+
containsHelpArgument args = any isHelpArgument ( map ( map toLower ) args )
2828

2929
-- | 'extractOutputFile' extracts the output file argument from a list of strings.
3030
extractOutputFile :: [[Char]] -> [Char]
31-
extractOutputFile args
32-
| "-o" == head args = head ( tail args )
33-
| otherwise = extractOutputFile ( tail args )
31+
extractOutputFile args = case args of
32+
[] -> ""
33+
("-o":[]) -> ""
34+
("-o":xs) -> head ( xs )
35+
_ -> extractOutputFile ( tail args )
3436

3537
-- | 'isHelpArgument' checks if a string is a help argument.
3638
isHelpArgument :: [Char] -> Bool
37-
isHelpArgument arg
38-
| '-' == ( head arg ) = isHelpArgument( tail arg )
39-
| otherwise = "help" == ( map toLower arg )
39+
isHelpArgument arg = case arg of
40+
[] -> False
41+
('-':[]) -> False
42+
('-':xs) -> isHelpArgument( xs )
43+
"help" -> True
44+
_ -> False

0 commit comments

Comments
 (0)