1 -- |The CommandLine module handles parsing of the command-line options.
2 -- It should more or less be a black box, providing Main with only the
3 -- information it requires.
13 import Data.Maybe (isJust, isNothing)
14 import System.Console.GetOpt
15 import System.Environment (getArgs)
19 -- |A record containing values for all available options.
20 data Options = Options { opt_help :: Bool,
21 opt_from :: Maybe String,
22 opt_to :: Maybe String }
25 -- |Constructs an instance of Options, with each of its members set to
27 default_options :: Options
28 default_options = Options { opt_help = False,
33 -- |The options list that we construct associates a function with each
34 -- option. This function is responsible for updating an Options record
35 -- with the appropriate value.
37 -- For more information and an example of this idiom, see,
39 -- <http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>
41 options :: [OptDescr (Options -> IO Options)]
43 [ Option ['h'][] (NoArg set_help) "Prints this help message.",
44 Option ['t'][] (ReqArg set_to "email_address") "Send tweets TO email_address.",
45 Option ['f'][] (ReqArg set_from "email_address") "Send tweets FROM email_address."
49 set_help :: Options -> IO Options
51 return opts { opt_help = True }
53 set_to :: String -> Options -> IO Options
55 return opts { opt_to = Just arg }
57 set_from :: String -> Options -> IO Options
58 set_from arg opts = do
59 return opts { opt_from = Just arg }
64 usage = "Usage: twat [-t to_address] [-f from_address] <username1> [username2, [username3]...]"
67 -- The usage header, and all available flags (as generated by GetOpt)
69 help_text = usageInfo usage options
72 -- Return a list of options.
73 parse_options :: IO Options
76 let (actions, _, _) = getOpt Permute options argv
78 -- This will execute each of the functions contained in our options
79 -- list, one after another, on a default_options record. The end
80 -- result should be an Options instance with all of its members set
82 opts <- foldl (>>=) (return default_options) actions
87 -- |Parse errors relating to the list of usernames.
88 username_errors :: IO [String]
91 let (_, usernames, _) = getOpt Permute options argv
94 then return ["No usernames provided."]
98 -- |Parse errors relating to the "To" address.
99 to_errors :: IO [String]
101 toaddr <- to_email_address
102 fromaddr <- from_email_address
103 if (isNothing toaddr) && (isJust fromaddr)
104 then return ["\"From\" address specified without \"To\" address."]
108 -- |Parse errors relating to the "From" address.
109 from_errors :: IO [String]
111 toaddr <- to_email_address
112 fromaddr <- from_email_address
113 if (isJust toaddr) && (isNothing fromaddr)
114 then return ["\"To\" address specified without \"From\" address."]
118 -- |Format an error message for printing.
119 format_error :: String -> String
120 format_error err = "ERROR: " ++ err ++ "\n"
123 -- |Return a list of all parse errors.
124 parse_errors :: IO [String]
127 let (_, _, errors) = getOpt Permute options argv
128 errs_username <- username_errors
130 errs_from <- from_errors
131 return $ map format_error (errors ++ errs_username ++ errs_to ++ errs_from)
133 -- |Was the "help" option passed on the command line?
136 opts <- parse_options
137 return (opt_help opts)
140 -- |What "To" address was given on the command line?
141 to_email_address :: IO (Maybe String)
142 to_email_address = do
143 opts <- parse_options
146 -- |What "From" address was given on the command line?
147 from_email_address :: IO (Maybe String)
148 from_email_address = do
149 opts <- parse_options
150 return (opt_from opts)
153 -- |What usernames were passed on the command line?
154 parse_usernames :: IO [String]
157 let (_, usernames, _) = getOpt Permute options argv