--- The usage header, and all available flags (as generated by GetOpt)
-help_text :: String
-help_text = usageInfo usage options
-
-
--- Return a list of options.
-parse_options :: IO Options
-parse_options = do
- argv <- getArgs
- let (actions, _, _) = getOpt Permute options argv
-
- -- This will execute each of the functions contained in our options
- -- list, one after another, on a default_options record. The end
- -- result should be an Options instance with all of its members set
- -- correctly.
- opts <- foldl (>>=) (return default_options) actions
-
- return opts
-
-
-username_errors :: IO [String]
-username_errors = do
- argv <- getArgs
- let (_, usernames, _) = getOpt Permute options argv
-
- if (null usernames)
- then return ["No usernames provided."]
- else return []
-
-
-to_errors :: IO [String]
-to_errors = do
- toaddr <- to_email_address
- fromaddr <- from_email_address
- if (isNothing toaddr) && (isJust fromaddr)
- then return ["\"From\" address specified without \"To\" address."]
- else return []
-
-from_errors :: IO [String]
-from_errors = do
- toaddr <- to_email_address
- fromaddr <- from_email_address
- if (isJust toaddr) && (isNothing fromaddr)
- then return ["\"To\" address specified without \"From\" address."]
- else return []
-
-
-format_error :: String -> String
-format_error err = "ERROR: " ++ err ++ "\n"
-
-
--- Return a list of errors.
-parse_errors :: IO [String]
-parse_errors = do
- argv <- getArgs
- let (_, _, errors) = getOpt Permute options argv
- errs_username <- username_errors
- errs_to <- to_errors
- errs_from <- from_errors
- return $ map format_error (errors ++ errs_username ++ errs_to ++ errs_from)
-
--- Is the help option set?
-help_set :: IO Bool
-help_set = do
- opts <- parse_options
- return (opt_help opts)
-
-
-to_email_address :: IO (Maybe String)
-to_email_address = do
- opts <- parse_options
- return (opt_to opts)
-
-from_email_address :: IO (Maybe String)
-from_email_address = do
- opts <- parse_options
- return (opt_from opts)
-
-
-parse_usernames :: IO [String]
-parse_usernames = do
- argv <- getArgs
- let (_, usernames, _) = getOpt Permute options argv
- return usernames
+parse_args :: IO (CmdArgs OptionalCfg)
+parse_args = do
+ x <- getArgs
+ let y = process arg_spec x
+ case y of
+ Right result -> return result
+ Left err -> do
+ hPutStrLn stderr err
+ exitWith (ExitFailure exit_args_parse_failed)
+
+
+-- | Really get the command-line arguments. This calls 'parse_args'
+-- first to replace the default "wrong number of arguments" error,
+-- and then runs 'cmdArgsApply' on the result to do what the
+-- 'cmdArgs' function usually does.
+apply_args :: IO OptionalCfg
+apply_args =
+ parse_args >>= cmdArgsApply