Skip to content

Instantly share code, notes, and snippets.

@dair-targ
Created February 12, 2014 13:19
Show Gist options
  • Save dair-targ/8955418 to your computer and use it in GitHub Desktop.
Save dair-targ/8955418 to your computer and use it in GitHub Desktop.
Reads values of property from file
import System.Environment
import Data.List
import Data.Maybe
data Arguments = Arguments String String
| UnexpectedArguments [String]
| NotEnoughArguments
deriving Show
parseArgs :: [String] -> Arguments
parseArgs ( path : k : [] ) = Arguments path k
parseArgs ( path : k : rest ) = UnexpectedArguments rest
parseArgs _ = NotEnoughArguments
data Property = Property String String
| MalformedProperty
deriving Show
match :: String -> Property -> Bool
match s ( Property k v ) = k == s
match s MalformedProperty = False
value :: Property -> String
value ( Property k v ) = v
parse :: String -> Property
parse s
| ( not $ null k ) && ( not $ null v ) = Property k v
| otherwise = MalformedProperty
where
v = drop 1 _v
( k , _v ) = break ( == '=' ) s
filter :: String -> [Property] -> [Property]
filter k pp = Data.List.filter ( match k ) pp
main = do
args <- getArgs
case parseArgs args of
Arguments path key -> do
contents <- readFile path
putStrLn $ foldl (++) "" $ foundValues key contents
where
foundValues k = map value . foundProperties k
foundProperties k = Main.filter k . properties
properties = map parse . lines
UnexpectedArguments aa -> do
putStrLn $ "Unexpected arguments: " ++ intercalate " " aa
NotEnoughArguments -> do
putStrLn "Not enough arguments"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment