]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/CommandLine.hs
919a3a2136f11a76a9da1441e36b7497084ad407
[dead/halcyon.git] / src / CommandLine.hs
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.
4 module CommandLine
5 ( heartbeat,
6 help_set,
7 help_text,
8 ignore_replies_set,
9 ignore_retweets_set,
10 from_email_address,
11 to_email_address,
12 parse_errors,
13 parse_usernames
14 ) where
15
16 import Data.Maybe (isJust, isNothing)
17 import System.Console.GetOpt
18 import System.Environment (getArgs)
19
20
21
22 -- |A record containing values for all available options.
23 data Options = Options { opt_heartbeat :: Maybe Int,
24 opt_help :: Bool,
25 opt_ignore_replies :: Bool,
26 opt_ignore_retweets :: Bool,
27 opt_from :: Maybe String,
28 opt_to :: Maybe String }
29
30
31 -- |Constructs an instance of Options, with each of its members set to
32 -- default values.
33 default_options :: Options
34 default_options = Options { opt_heartbeat = Just 600,
35 opt_help = False,
36 opt_ignore_replies = False,
37 opt_ignore_retweets = False,
38 opt_from = Nothing,
39 opt_to = Nothing }
40
41
42 -- |The options list that we construct associates a function with each
43 -- option. This function is responsible for updating an Options record
44 -- with the appropriate value.
45 --
46 -- For more information and an example of this idiom, see,
47 --
48 -- <http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>
49 --
50 options :: [OptDescr (Options -> IO Options)]
51 options =
52 [ Option
53 ['h']["help"]
54 (NoArg set_help)
55 "Prints this help message.",
56
57 Option
58 ['n']["heartbeat"]
59 (ReqArg set_heartbeat "heartbeat")
60 "How many seconds to wait between polling.",
61
62 Option
63 ['t']["to"]
64 (ReqArg set_to "email_address")
65 "Send tweets TO email_address.",
66
67 Option
68 ['f']["from"]
69 (ReqArg set_from "email_address")
70 "Send tweets FROM email_address.",
71
72 Option
73 ['i']["ignore-replies"]
74 (NoArg set_ignore_replies)
75 "Ignore replies.",
76
77 Option
78 ['I']["ignore-retweets"]
79 (NoArg set_ignore_retweets)
80 "Ignore retweets."
81 ]
82
83
84 -- | Attempt to parse an 'Int' from a 'String'. This is just a 'Maybe'
85 -- wrapper around 'reads'.
86 parse_int :: String -> Maybe Int
87 parse_int s =
88 case (reads s) of
89 [(n,_)] -> Just n
90 _ -> Nothing
91
92 set_heartbeat :: String -> Options -> IO Options
93 set_heartbeat arg opts = do
94 let new_heartbeat = parse_int arg
95 return opts { opt_heartbeat = new_heartbeat }
96
97 set_help :: Options -> IO Options
98 set_help opts = do
99 return opts { opt_help = True }
100
101 set_ignore_retweets :: Options -> IO Options
102 set_ignore_retweets opts =
103 return opts { opt_ignore_retweets = True }
104
105 set_ignore_replies :: Options -> IO Options
106 set_ignore_replies opts =
107 return opts { opt_ignore_replies = True }
108
109 set_to :: String -> Options -> IO Options
110 set_to arg opts = do
111 return opts { opt_to = Just arg }
112
113 set_from :: String -> Options -> IO Options
114 set_from arg opts = do
115 return opts { opt_from = Just arg }
116
117
118 -- The usage header.
119 usage :: String
120 usage = "Usage: twat [-n heartbeat] [-t to_address] [-f from_address] <username1> [username2, [username3]...]"
121
122
123 -- The usage header, and all available flags (as generated by GetOpt)
124 help_text :: String
125 help_text = usageInfo usage options
126
127
128 -- Return a list of options.
129 parse_options :: IO Options
130 parse_options = do
131 argv <- getArgs
132 let (actions, _, _) = getOpt Permute options argv
133
134 -- This will execute each of the functions contained in our options
135 -- list, one after another, on a default_options record. The end
136 -- result should be an Options instance with all of its members set
137 -- correctly.
138 opts <- foldl (>>=) (return default_options) actions
139
140 return opts
141
142
143 -- | A list of parse errors relating to the heartbeat.
144 heartbeat_errors :: IO [String]
145 heartbeat_errors = do
146 hb <- heartbeat
147 if (isNothing hb)
148 then return ["\"heartbeat\" does not appear to be an integer."]
149 else return []
150
151 -- |Parse errors relating to the list of usernames.
152 username_errors :: IO [String]
153 username_errors = do
154 argv <- getArgs
155 let (_, usernames, _) = getOpt Permute options argv
156
157 if (null usernames)
158 then return ["no usernames provided."]
159 else return []
160
161
162 -- |Parse errors relating to the "To" address.
163 to_errors :: IO [String]
164 to_errors = do
165 toaddr <- to_email_address
166 fromaddr <- from_email_address
167 if (isNothing toaddr) && (isJust fromaddr)
168 then return ["\"from\" address specified without \"to\" address."]
169 else return []
170
171
172 -- |Parse errors relating to the "From" address.
173 from_errors :: IO [String]
174 from_errors = do
175 toaddr <- to_email_address
176 fromaddr <- from_email_address
177 if (isJust toaddr) && (isNothing fromaddr)
178 then return ["\"to\" address specified without \"from\" address."]
179 else return []
180
181
182 -- |Format an error message for printing.
183 format_error :: String -> String
184 format_error err = "ERROR: " ++ err ++ "\n"
185
186
187 -- |Return a list of all parse errors.
188 parse_errors :: IO [String]
189 parse_errors = do
190 argv <- getArgs
191 let (_, _, errors) = getOpt Permute options argv
192 errs_heartbeat <- heartbeat_errors
193 errs_username <- username_errors
194 errs_to <- to_errors
195 errs_from <- from_errors
196 return $ map format_error (errors ++
197 errs_heartbeat ++
198 errs_username ++
199 errs_to ++
200 errs_from)
201
202 -- |Was the "help" option passed on the command line?
203 help_set :: IO Bool
204 help_set = do
205 opts <- parse_options
206 return (opt_help opts)
207
208 -- | Was the "ignore-replies" option passes on the command line?
209 ignore_replies_set :: IO Bool
210 ignore_replies_set = do
211 opts <- parse_options
212 return (opt_ignore_replies opts)
213
214 -- | Was the "ignore-retweets" option passes on the command line?
215 ignore_retweets_set :: IO Bool
216 ignore_retweets_set = do
217 opts <- parse_options
218 return (opt_ignore_retweets opts)
219
220 -- |What's the heartbeat?
221 heartbeat :: IO (Maybe Int)
222 heartbeat = do
223 opts <- parse_options
224 return (opt_heartbeat opts)
225
226 -- |What "To" address was given on the command line?
227 to_email_address :: IO (Maybe String)
228 to_email_address = do
229 opts <- parse_options
230 return (opt_to opts)
231
232 -- |What "From" address was given on the command line?
233 from_email_address :: IO (Maybe String)
234 from_email_address = do
235 opts <- parse_options
236 return (opt_from opts)
237
238
239 -- |What usernames were passed on the command line?
240 parse_usernames :: IO [String]
241 parse_usernames = do
242 argv <- getArgs
243 let (_, usernames, _) = getOpt Permute options argv
244 return usernames