]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/CommandLine.hs
Fix remaining hlint suggestions.
[dead/halcyon.git] / src / CommandLine.hs
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.
4 module CommandLine
5 ( get_cfg,
6 help_set,
7 help_text,
8 parse_errors,
9 parse_usernames
10 ) where
11
12 import Data.Maybe (fromJust, isJust, isNothing)
13 import System.Console.GetOpt
14 import System.Directory (doesFileExist)
15 import System.Environment (getArgs)
16
17 import Configuration (Cfg(..))
18
19 -- | A record containing values for all available options.
20 data Options = Options { opt_consumer_key :: Maybe String,
21 opt_consumer_secret :: Maybe String,
22 opt_access_token :: Maybe String,
23 opt_access_secret :: Maybe String,
24 opt_heartbeat :: Maybe Int,
25 opt_help :: Bool,
26 opt_ignore_replies :: Bool,
27 opt_ignore_retweets :: Bool,
28 opt_sendmail_path :: FilePath,
29 opt_from :: Maybe String,
30 opt_to :: Maybe String,
31 opt_verbose :: Bool }
32
33
34 -- | Constructs an instance of Options, with each of its members set
35 -- to default values.
36 default_options :: Options
37 default_options = Options { opt_access_token = Nothing,
38 opt_access_secret = Nothing,
39 opt_consumer_key = Nothing,
40 opt_consumer_secret = Nothing,
41 opt_heartbeat = Just 600,
42 opt_help = False,
43 opt_ignore_replies = False,
44 opt_ignore_retweets = False,
45 opt_sendmail_path = "/usr/sbin/sendmail",
46 opt_from = Nothing,
47 opt_to = Nothing,
48 opt_verbose = False }
49
50
51 -- | The options list that we construct associates a function with
52 -- each option. This function is responsible for updating an Options
53 -- record with the appropriate value.
54 --
55 -- For more information and an example of this idiom, see,
56 --
57 -- <http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>
58 --
59 options :: [OptDescr (Options -> IO Options)]
60 options =
61 [ Option
62 "" ["consumer-key"]
63 (ReqArg set_consumer_key "consumer-key")
64 "Your Twitter API consumer key.",
65
66 Option
67 "" ["consumer-secret"]
68 (ReqArg set_consumer_secret "consumer-secret")
69 "Your Twitter API consumer secret.",
70
71 Option
72 "" ["access-token"]
73 (ReqArg set_access_token "access-token")
74 "Your Twitter API access token.",
75
76 Option
77 "" ["access-secret"]
78 (ReqArg set_access_secret "access-secret")
79 "Your Twitter API access secret.",
80
81 Option
82 "h" ["help"]
83 (NoArg set_help)
84 "Prints this help message.",
85
86 Option
87 "n" ["heartbeat"]
88 (ReqArg set_heartbeat "heartbeat")
89 "How many seconds to wait between polling.",
90
91 Option
92 "t" ["to"]
93 (ReqArg set_to "email_address")
94 "Send tweets TO email_address.",
95
96 Option
97 "f" ["from"]
98 (ReqArg set_from "email_address")
99 "Send tweets FROM email_address.",
100
101 Option
102 "s" ["sendmail_path"]
103 (ReqArg set_sendmail_path "sendmail_path")
104 "Use sendmail_path to send mail",
105
106 Option
107 "i" ["ignore-replies"]
108 (NoArg set_ignore_replies)
109 "Ignore replies.",
110
111 Option
112 "I" ["ignore-retweets"]
113 (NoArg set_ignore_retweets)
114 "Ignore retweets.",
115
116 Option
117 "v" ["verbose"]
118 (NoArg set_verbose)
119 "Be verbose about stuff."
120 ]
121
122
123 -- | Attempt to parse an 'Int' from a 'String'. This is just a 'Maybe'
124 -- wrapper around 'reads'.
125 parse_int :: String -> Maybe Int
126 parse_int s =
127 case (reads s) of
128 [(n,_)] -> Just n
129 _ -> Nothing
130
131 set_consumer_key :: String -> Options -> IO Options
132 set_consumer_key arg opts =
133 return opts { opt_consumer_key = Just arg }
134
135 set_consumer_secret :: String -> Options -> IO Options
136 set_consumer_secret arg opts =
137 return opts { opt_consumer_secret = Just arg }
138
139 set_access_token :: String -> Options -> IO Options
140 set_access_token arg opts =
141 return opts { opt_access_token = Just arg }
142
143 set_access_secret :: String -> Options -> IO Options
144 set_access_secret arg opts =
145 return opts { opt_access_secret = Just arg }
146
147 set_heartbeat :: String -> Options -> IO Options
148 set_heartbeat arg opts = do
149 let new_heartbeat = parse_int arg
150 return opts { opt_heartbeat = new_heartbeat }
151
152 set_help :: Options -> IO Options
153 set_help opts =
154 return opts { opt_help = True }
155
156 set_ignore_retweets :: Options -> IO Options
157 set_ignore_retweets opts =
158 return opts { opt_ignore_retweets = True }
159
160 set_ignore_replies :: Options -> IO Options
161 set_ignore_replies opts =
162 return opts { opt_ignore_replies = True }
163
164 set_verbose :: Options -> IO Options
165 set_verbose opts =
166 return opts { opt_verbose = True }
167
168 set_sendmail_path :: String -> Options -> IO Options
169 set_sendmail_path arg opts =
170 return opts { opt_sendmail_path = arg }
171
172 set_to :: String -> Options -> IO Options
173 set_to arg opts =
174 return opts { opt_to = Just arg }
175
176 set_from :: String -> Options -> IO Options
177 set_from arg opts =
178 return opts { opt_from = Just arg }
179
180
181 -- | The usage header.
182 usage :: String
183 usage = "Usage: twat --consumer-key=<key> --consumer-secret=<secret> --access-token=<key> --access-secret=<secret> [-n heartbeat] [-t to_address] [-f from_address] [-s path-to-sendmail] <username1> [username2, [username3]...]"
184
185
186 -- | Was the help option passed?
187 help_set :: IO Bool
188 help_set = do
189 opts <- parse_options
190 return (opt_help opts)
191
192 -- | The usage header, and all available flags (as generated by GetOpt)
193 help_text :: String
194 help_text = usageInfo usage options
195
196
197 -- | Return a list of options.
198 parse_options :: IO Options
199 parse_options = do
200 argv <- getArgs
201 let (actions, _, _) = getOpt Permute options argv
202
203 -- This will execute each of the functions contained in our options
204 -- list, one after another, on a default_options record. The end
205 -- result should be an Options instance with all of its members set
206 -- correctly.
207 foldl (>>=) (return default_options) actions
208
209
210 -- | A list of parse errors relating to the heartbeat.
211 heartbeat_errors :: IO [String]
212 heartbeat_errors = do
213 hb <- parse_heartbeat
214 return ["\"heartbeat\" does not appear to be an integer." | isNothing hb ]
215
216 -- | Parse errors relating to the list of usernames.
217 username_errors :: IO [String]
218 username_errors = do
219 argv <- getArgs
220 let (_, usernames, _) = getOpt Permute options argv
221 return [ "no usernames provided." | null usernames ]
222
223
224 -- | Parse errors relating to the "To" address.
225 to_errors :: IO [String]
226 to_errors = do
227 toaddr <- parse_to_address
228 fromaddr <- parse_from_address
229 return ["\"from\" address specified without \"to\" address."
230 | (isNothing toaddr) && (isJust fromaddr) ]
231
232
233 -- | Errors for the sendmail path argument.
234 sendmail_path_errors :: IO [String]
235 sendmail_path_errors = do
236 sendmail <- parse_sendmail_path
237 exists <- doesFileExist sendmail
238 return [ "sendmail path does not exist" | not exists ]
239
240
241 -- | Parse errors relating to the "From" address.
242 from_errors :: IO [String]
243 from_errors = do
244 toaddr <- parse_to_address
245 fromaddr <- parse_from_address
246 return [ "\"to\" address specified without \"from\" address."
247 | (isJust toaddr) && (isNothing fromaddr) ]
248
249
250 -- | Format an error message for printing.
251 format_error :: String -> String
252 format_error err = "ERROR: " ++ err ++ "\n"
253
254
255 -- | Return a list of all parse errors.
256 parse_errors :: IO [String]
257 parse_errors = do
258 argv <- getArgs
259 let (_, _, errors) = getOpt Permute options argv
260 errs_heartbeat <- heartbeat_errors
261 errs_username <- username_errors
262 errs_to <- to_errors
263 errs_from <- from_errors
264 errs_sendmail <- sendmail_path_errors
265 return $ map format_error (errors ++
266 errs_heartbeat ++
267 errs_username ++
268 errs_sendmail ++
269 errs_to ++
270 errs_from)
271
272 -- | What's the heartbeat?
273 parse_heartbeat :: IO (Maybe Int)
274 parse_heartbeat = do
275 opts <- parse_options
276 return (opt_heartbeat opts)
277
278 -- | What "To" address was given on the command line?
279 parse_to_address :: IO (Maybe String)
280 parse_to_address = do
281 opts <- parse_options
282 return (opt_to opts)
283
284 -- | What sendmail path was given on the command line?
285 parse_sendmail_path :: IO FilePath
286 parse_sendmail_path = do
287 opts <- parse_options
288 return (opt_sendmail_path opts)
289
290 -- | What "From" address was given on the command line?
291 parse_from_address :: IO (Maybe String)
292 parse_from_address = do
293 opts <- parse_options
294 return (opt_from opts)
295
296
297 -- | What usernames were passed on the command line?
298 parse_usernames :: IO [String]
299 parse_usernames = do
300 argv <- getArgs
301 let (_, usernames, _) = getOpt Permute options argv
302 return usernames
303
304
305
306 -- | Construct a Cfg object from the command line options assuming
307 -- there are no errors.
308 get_cfg :: IO Cfg
309 get_cfg = do
310 opts <- parse_options
311 return Cfg { consumer_key = fromJust $ opt_consumer_key opts,
312 consumer_secret = fromJust $ opt_consumer_secret opts,
313 access_token = fromJust $ opt_access_token opts,
314 access_secret = fromJust $ opt_access_secret opts,
315 heartbeat = fromJust $ opt_heartbeat opts,
316 ignore_replies = opt_ignore_replies opts,
317 ignore_retweets = opt_ignore_retweets opts,
318 sendmail_path = opt_sendmail_path opts,
319 from_address = opt_from opts,
320 to_address = opt_to opts,
321 verbose = opt_verbose opts }