1 -- | The CommandLine module handles parsing of the command-line
2 -- options. It should more or less be a black box, providing Main
3 -- with only the information it requires.
12 import Data.Maybe (fromJust, isJust, isNothing)
13 import System.Console.GetOpt
14 import System.Environment (getArgs)
16 import Configuration (Cfg(..))
18 -- | A record containing values for all available options.
19 data Options = Options { opt_heartbeat :: Maybe Int,
21 opt_ignore_replies :: Bool,
22 opt_ignore_retweets :: Bool,
23 opt_from :: Maybe String,
24 opt_to :: Maybe String,
28 -- | Constructs an instance of Options, with each of its members set
30 default_options :: Options
31 default_options = Options { opt_heartbeat = Just 600,
33 opt_ignore_replies = False,
34 opt_ignore_retweets = False,
40 -- | The options list that we construct associates a function with
41 -- each option. This function is responsible for updating an Options
42 -- record with the appropriate value.
44 -- For more information and an example of this idiom, see,
46 -- <http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>
48 options :: [OptDescr (Options -> IO Options)]
53 "Prints this help message.",
57 (ReqArg set_heartbeat "heartbeat")
58 "How many seconds to wait between polling.",
62 (ReqArg set_to "email_address")
63 "Send tweets TO email_address.",
67 (ReqArg set_from "email_address")
68 "Send tweets FROM email_address.",
71 ['i']["ignore-replies"]
72 (NoArg set_ignore_replies)
76 ['I']["ignore-retweets"]
77 (NoArg set_ignore_retweets)
83 "Be verbose about stuff."
87 -- | Attempt to parse an 'Int' from a 'String'. This is just a 'Maybe'
88 -- wrapper around 'reads'.
89 parse_int :: String -> Maybe Int
95 set_heartbeat :: String -> Options -> IO Options
96 set_heartbeat arg opts = do
97 let new_heartbeat = parse_int arg
98 return opts { opt_heartbeat = new_heartbeat }
100 set_help :: Options -> IO Options
102 return opts { opt_help = True }
104 set_ignore_retweets :: Options -> IO Options
105 set_ignore_retweets opts =
106 return opts { opt_ignore_retweets = True }
108 set_ignore_replies :: Options -> IO Options
109 set_ignore_replies opts =
110 return opts { opt_ignore_replies = True }
112 set_verbose :: Options -> IO Options
114 return opts { opt_verbose = True }
116 set_to :: String -> Options -> IO Options
118 return opts { opt_to = Just arg }
120 set_from :: String -> Options -> IO Options
121 set_from arg opts = do
122 return opts { opt_from = Just arg }
125 -- | The usage header.
127 usage = "Usage: twat [-n heartbeat] [-t to_address] [-f from_address] <username1> [username2, [username3]...]"
130 -- | Was the help option passed?
133 opts <- parse_options
134 return (opt_help opts)
136 -- | The usage header, and all available flags (as generated by GetOpt)
138 help_text = usageInfo usage options
141 -- | Return a list of options.
142 parse_options :: IO Options
145 let (actions, _, _) = getOpt Permute options argv
147 -- This will execute each of the functions contained in our options
148 -- list, one after another, on a default_options record. The end
149 -- result should be an Options instance with all of its members set
151 opts <- foldl (>>=) (return default_options) actions
156 -- | A list of parse errors relating to the heartbeat.
157 heartbeat_errors :: IO [String]
158 heartbeat_errors = do
159 hb <- parse_heartbeat
161 then return ["\"heartbeat\" does not appear to be an integer."]
164 -- |Parse errors relating to the list of usernames.
165 username_errors :: IO [String]
168 let (_, usernames, _) = getOpt Permute options argv
171 then return ["no usernames provided."]
175 -- |Parse errors relating to the "To" address.
176 to_errors :: IO [String]
178 toaddr <- parse_to_address
179 fromaddr <- parse_from_address
180 if (isNothing toaddr) && (isJust fromaddr)
181 then return ["\"from\" address specified without \"to\" address."]
185 -- | Parse errors relating to the "From" address.
186 from_errors :: IO [String]
188 toaddr <- parse_to_address
189 fromaddr <- parse_from_address
190 if (isJust toaddr) && (isNothing fromaddr)
191 then return ["\"to\" address specified without \"from\" address."]
195 -- | Format an error message for printing.
196 format_error :: String -> String
197 format_error err = "ERROR: " ++ err ++ "\n"
200 -- | Return a list of all parse errors.
201 parse_errors :: IO [String]
204 let (_, _, errors) = getOpt Permute options argv
205 errs_heartbeat <- heartbeat_errors
206 errs_username <- username_errors
208 errs_from <- from_errors
209 return $ map format_error (errors ++
215 -- | What's the heartbeat?
216 parse_heartbeat :: IO (Maybe Int)
218 opts <- parse_options
219 return (opt_heartbeat opts)
221 -- | What "To" address was given on the command line?
222 parse_to_address :: IO (Maybe String)
223 parse_to_address = do
224 opts <- parse_options
227 -- | What "From" address was given on the command line?
228 parse_from_address :: IO (Maybe String)
229 parse_from_address = do
230 opts <- parse_options
231 return (opt_from opts)
234 -- | What usernames were passed on the command line?
235 parse_usernames :: IO [String]
238 let (_, usernames, _) = getOpt Permute options argv
243 -- | Construct a Cfg object from the command line options assuming
244 -- there are no errors.
247 opts <- parse_options
248 return Cfg { heartbeat = fromJust $ opt_heartbeat opts,
249 ignore_replies = opt_ignore_replies opts,
250 ignore_retweets = opt_ignore_retweets opts,
251 from_address = opt_from opts,
252 to_address = opt_to opts,
253 verbose = opt_verbose opts }