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