--- This constructs an instance of Options, with each of its members
--- set to default values.
-default_options :: Options
-default_options = Options { opt_help = False,
- opt_from = Nothing,
- opt_to = Nothing }
-
-
--- The options list that we construct associates a function with each
--- option. This function is responsible for updating an Options record
--- with the appropriate value.
---
--- For more information and an example of this idiom, see,
---
--- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
---
-options :: [OptDescr (Options -> IO Options)]
-options =
- [ Option ['h'][] (NoArg set_help) "Prints this help message.",
- Option ['t'][] (ReqArg set_to "email_address") "Send tweets TO email_address.",
- Option ['f'][] (ReqArg set_from "email_address") "Send tweets FROM email_address."
- ]
-
-
-set_help :: Options -> IO Options
-set_help opts = do
- return opts { opt_help = True }
-
-set_to :: String -> Options -> IO Options
-set_to arg opts = do
- return opts { opt_to = Just arg }
-
-set_from :: String -> Options -> IO Options
-set_from arg opts = do
- return opts { opt_from = Just arg }
-
-
--- The usage header.
-usage :: String
-usage = "Usage: twat [-t to_address] [-f from_address] <username1> [username2, [username3]...]"
-
-
--- 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