Skip to content

Commit

Permalink
Make arg parsing more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Stokes committed Jun 1, 2009
1 parent 0d02283 commit 957c0a6
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions main.hs
Expand Up @@ -31,18 +31,28 @@ options = [Option ['n'] ["max"] (ReqArg read "NUM") "max results (default 15)"]
parseArgs :: [String] -> IO SearchOptions
parseArgs argv =
case getOpt' Permute options argv of
([count], term:terms, [], []) -> return SearchOptions {
resultsPerPage = count,
searchQuery = unwords (term:terms)
}
([], term:terms, [], []) -> return $ basicOptions $ unwords (term:terms)
(_, _, unrecog:unrecogs, _) -> do
sequence $ map unrecognised (unrecog:unrecogs)
err $ usageInfo usage options
(_, _, _, errr:errrs) -> do
sequence $ map putStrLn (errr:errrs)
err $ usageInfo usage options
_ -> err $ usageInfo usage options
([count], term:terms, [] , [] ) ->
return SearchOptions {
resultsPerPage = count,
searchQuery = unwords (term:terms)
}

([] , term:terms, [] , [] ) ->
return $ basicOptions $ unwords (term:terms)

(_ , _ , unrecog:unrecogs, _ ) ->
do
sequence $ map unrecognised (unrecog:unrecogs)
errUsage

(_ , _ , _ , errr:errrs) ->
do
sequence $ map putStrLn (errr:errrs)
errUsage

_ -> errUsage
where
errUsage = err $ usageInfo usage options

unrecognised :: String -> IO ()
unrecognised opt = putStrLn ("Unrecognised option " ++ opt)
Expand Down

0 comments on commit 957c0a6

Please sign in to comment.