parseArgs :: [String] -> IO (Flags, [String])
parseArgs args
  | flagHelp flags  = return (flags, args')
  | not (null errs) = printErrors errs
  | otherwise       = return (flags, args')

  where
    (flags0, args', errs) = getOpt Permute flagDescrs args
    flags = foldr (flip (.)) id flags0 defaultFlags

    printErrors errs = do
      putStrLn $ concat errs ++ "Try --help."
      exitFailure
parseArgs :: [String] -> IO (Flags, [String])
parseArgs args
  | flagHelp flags  = return (flags, args')
  | not (null errs) = printErrors errs
  | otherwise       = return (flags, args')

  where
    (flags0, args', errs) = getOpt Permute flagDescrs args
    flags = foldr (flip (.)) id flags0 defaultFlags

    printErrors errs = do
      putStrLn $ concat errs ++ "Try --help."
      exitFailure
parseArgs args
  | flagHelp flags  = return (flags, args')
  | not (null errs) = printErrors errs
  | otherwise       = return (flags, args')

  where
    (flags0, args', errs) = getOpt Permute flagDescrs args
    flags = foldr (flip (.)) id flags0 defaultFlags

    printErrors errs = do
      putStrLn $ concat errs ++ "Try --help."
      exitFailure