import Control.Monad import System type Arg = (String, String) type Args = [Arg] defaultArgs :: Args defaultArgs = [ ("-inputXMLPath", "localization_client.txt"), ("-outputXMLPath", "locale"), ("-outputXMLName", "localization"), ("-packagePath", "") ] parseArgs :: Args -> [String] -> (Args, [String]) parseArgs defaults rawArgs = parse defaults rawArgs [] where parse args [] errs = (args, errs) parse args ( x:[]) errs = (args, ("Error: alone argument option " ++ x):errs) parse args (k:v:tl) errs = case tryReplace k v args of Just args -> parse args tl errs Nothing -> parse args tl (("Error: unknown option " ++ k):errs) tryReplace :: String -> String -> Args -> Maybe Args tryReplace _ _ [] = Nothing tryReplace key val ((k, v):rest) = if key == k then Just ((key, val):rest) else case tryReplace key val rest of Just xs -> Just ((k, v):xs) Nothing -> Nothing main = do rawArgs <- getArgs let (args, errs) = parseArgs defaultArgs rawArgs forM_ (reverse errs) putStrLn putStrLn "Parameters:" forM_ args $ \(arg, val) -> putStrLn (arg ++ " = " ++ val)