From: gburri Date: Mon, 6 Apr 2009 21:51:35 +0000 (+0000) Subject: ADD treatement of the arguments X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=4c7939a757a98c33339c48482ffc1708e9662809;p=pompage.git ADD treatement of the arguments git-svn-id: svn://euphorik.ch/pompage@80 02bbb61a-6d21-0410-aba0-cb053bdfd66a --- diff --git a/src/Pompage.hs b/src/Pompage.hs index b986e99..200da4c 100644 --- a/src/Pompage.hs +++ b/src/Pompage.hs @@ -1,6 +1,7 @@ import System.IO (readFile, FilePath(..)) -import System.Environment (getArgs) +import System.Environment (getArgs, getProgName) import Data.List +import Text.Printf (printf) import Text.XML.Light type Movies = [Movie] @@ -23,27 +24,41 @@ data Movie = Movie { , url :: String } deriving (Show) -data Args = Args { - xml :: String - , moviesDir :: FilePath -} deriving (Show) +data Arg = XML | MoviesDir deriving (Show, Eq) +type Args = [(Arg, String)] -test = do +main = do args <- getArgs - case readArgs args of - Left mess -> print mess - Right args -> do - paths <- moviePaths $ moviesDir args + progName <- getProgName + case checkArgs $ readArgs args of + Nothing -> printf usage progName + Just args -> do + let dir = case lookup MoviesDir args of + Just d -> d + Nothing -> "." + paths <- moviePaths dir file <- readFile "../xml/test.xml" print $ parseXMLDoc file movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"] - -readArgs :: [String] -> Either String Args -readArgs plop = undefined -{--readArgs (name:value:rest) = case name of - "-x" -> { xml = value } - "-d" -> { moviesDir = value }--} +usage = "Usage : %s -x [-d ]\n" + +checkArgs :: Maybe Args -> Maybe Args +checkArgs Nothing = Nothing +checkArgs (Just args) = if any (\(a, _) -> a == XML) args + then Just args + else Nothing + +readArgs :: [String] -> Maybe Args +readArgs (name:value:rest) + | name == "-x" = (XML, value) <:> readArgs rest + | name == "-d" = (MoviesDir, value) <:> readArgs rest + | otherwise = Nothing + where + _ <:> Nothing = Nothing + arg <:> Just args = Just $ arg : args +readArgs (_:[]) = Nothing +readArgs (_) = Just [] moviePaths :: FilePath -> IO [FilePath] moviePaths dir = undefined