--- |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
-
-
--- |Parse errors relating to the list of usernames.
-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 []
-
-
--- |Parse errors relating to the "To" address.
-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 []
-
-
--- |Parse errors relating to the "From" address.
-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 []