]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/CommandLine.hs
da6a04b06b35e2f2cdf3472bb68d53311611e05c
[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 ( help_set,
6 help_text,
7 from_email_address,
8 to_email_address,
9 parse_errors,
10 parse_usernames
11 ) where
12
13 import Data.Maybe (isJust, isNothing)
14 import System.Console.GetOpt
15 import System.Environment (getArgs)
16
17
18
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 }
23
24
25 -- |Constructs an instance of Options, with each of its members set to
26 -- default values.
27 default_options :: Options
28 default_options = Options { opt_help = False,
29 opt_from = Nothing,
30 opt_to = Nothing }
31
32
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.
36 --
37 -- For more information and an example of this idiom, see,
38 --
39 -- <http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>
40 --
41 options :: [OptDescr (Options -> IO Options)]
42 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."
46 ]
47
48
49 set_help :: Options -> IO Options
50 set_help opts = do
51 return opts { opt_help = True }
52
53 set_to :: String -> Options -> IO Options
54 set_to arg opts = do
55 return opts { opt_to = Just arg }
56
57 set_from :: String -> Options -> IO Options
58 set_from arg opts = do
59 return opts { opt_from = Just arg }
60
61
62 -- The usage header.
63 usage :: String
64 usage = "Usage: twat [-t to_address] [-f from_address] <username1> [username2, [username3]...]"
65
66
67 -- The usage header, and all available flags (as generated by GetOpt)
68 help_text :: String
69 help_text = usageInfo usage options
70
71
72 -- Return a list of options.
73 parse_options :: IO Options
74 parse_options = do
75 argv <- getArgs
76 let (actions, _, _) = getOpt Permute options argv
77
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
81 -- correctly.
82 opts <- foldl (>>=) (return default_options) actions
83
84 return opts
85
86
87 -- |Parse errors relating to the list of usernames.
88 username_errors :: IO [String]
89 username_errors = do
90 argv <- getArgs
91 let (_, usernames, _) = getOpt Permute options argv
92
93 if (null usernames)
94 then return ["No usernames provided."]
95 else return []
96
97
98 -- |Parse errors relating to the "To" address.
99 to_errors :: IO [String]
100 to_errors = do
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."]
105 else return []
106
107
108 -- |Parse errors relating to the "From" address.
109 from_errors :: IO [String]
110 from_errors = do
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."]
115 else return []
116
117
118 -- |Format an error message for printing.
119 format_error :: String -> String
120 format_error err = "ERROR: " ++ err ++ "\n"
121
122
123 -- |Return a list of all parse errors.
124 parse_errors :: IO [String]
125 parse_errors = do
126 argv <- getArgs
127 let (_, _, errors) = getOpt Permute options argv
128 errs_username <- username_errors
129 errs_to <- to_errors
130 errs_from <- from_errors
131 return $ map format_error (errors ++ errs_username ++ errs_to ++ errs_from)
132
133 -- |Was the "help" option passed on the command line?
134 help_set :: IO Bool
135 help_set = do
136 opts <- parse_options
137 return (opt_help opts)
138
139
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
144 return (opt_to opts)
145
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)
151
152
153 -- |What usernames were passed on the command line?
154 parse_usernames :: IO [String]
155 parse_usernames = do
156 argv <- getArgs
157 let (_, usernames, _) = getOpt Permute options argv
158 return usernames