]> gitweb.michael.orlitzky.com - hath.git/blob - src/CommandLine.hs
Add a test for the Cidr 'enumerate' function.
[hath.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 ( help_set,
6 help_text,
7 input_function,
8 Mode(..),
9 parse_errors,
10 parse_mode)
11 where
12
13 import Data.Char (toLower)
14 import System.Console.GetOpt (
15 ArgDescr(NoArg, ReqArg),
16 ArgOrder(Permute),
17 OptDescr(..),
18 getOpt,
19 usageInfo )
20 import System.Environment (getArgs)
21
22
23 -- | Lowercase an entire string.
24 lowercase :: String -> String
25 lowercase = map toLower
26
27
28 -- | The application currently has six modes. The default, Regex,
29 -- will compute a regular expression matching the input
30 -- CIDRs.
31 --
32 -- Reduce, on the other hand, will combine any redundant/adjacent
33 -- CIDR blocks into one.
34 --
35 -- Dupe will show you what would be removed by Reduce.
36 --
37 -- Diff will show both additions and deletions in a diff-like
38 -- format.
39 --
40 -- List will enumerate the IP addresses contained within the input
41 -- CIDRs.
42 --
43 -- Reverse will perform a reverse DNS (PTR) lookup on each IP
44 -- address contained within the input CIDRs.
45 --
46 data Mode = Regex | Reduce | Dupe | Diff | List | Reverse
47
48
49 -- | A record containing values for all available options.
50 data Options = Options { opt_help :: Bool,
51 opt_input :: IO String }
52
53
54 -- | This constructs an instance of Options, with each of its members
55 -- set to default values.
56 default_options :: Options
57 default_options = Options { opt_help = False,
58 opt_input = getContents }
59
60
61 -- | The options list that we construct associates a function with
62 -- each option. This function is responsible for updating an Options
63 -- record with the appropriate value.
64 --
65 -- For more information and an example of this idiom, see,
66 --
67 -- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
68 --
69 options :: [OptDescr (Options -> IO Options)]
70 options =
71 [ Option "h" ["help"] (NoArg set_help) "Prints this help message.",
72 Option "i" ["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
73
74 -- | Takes an Options as an argument, and sets its opt_help member to
75 -- True.
76 set_help :: Options -> IO Options
77 set_help opts =
78 return opts { opt_help = True }
79
80
81 -- | If the input file option is set, this function will update the
82 -- passed Options record with a new function for opt_input. The
83 -- default opt_input is to read from stdin, but if this option is
84 -- set, we replace that with readFile.
85 set_input :: String -> Options -> IO Options
86 set_input arg opts =
87 return opts { opt_input = readFile arg }
88
89
90 -- | The usage header.
91 usage :: String
92 usage =
93 "Usage: hath " ++
94 "[regexed|reduced|duped|diffed|listed|reversed] " ++
95 "[-h] " ++
96 "[-i FILE] " ++
97 "<input>"
98
99
100 -- | The usage header, and all available flags (as generated by GetOpt).
101 help_text :: String
102 help_text = usageInfo usage options
103
104
105 -- | Return a list of options.
106 parse_options :: IO Options
107 parse_options = do
108 argv <- getArgs
109 let (actions, _, _) = getOpt Permute options argv
110
111 -- This will execute each of the functions contained in our options
112 -- list, one after another, on a default_options record. The end
113 -- result should be an Options instance with all of its members set
114 -- correctly.
115 foldl (>>=) (return default_options) actions
116
117
118
119 -- | Return the mode if one was given.
120 parse_mode :: IO Mode
121 parse_mode = do
122 argv <- getArgs
123 let (_, non_options, _) = getOpt Permute options argv
124 return $ case non_options of
125 -- Default
126 [] -> Regex
127 -- Some non-option was given, but were any of them modes?
128 (x:_) ->
129 case (lowercase x) of
130 "regex" -> Regex
131 "regexed" -> Regex
132 "reduce" -> Reduce
133 "reduced" -> Reduce
134 "dupe" -> Dupe
135 "duped" -> Dupe
136 "diff" -> Diff
137 "diffed" -> Diff
138 "list" -> List
139 "listed" -> List
140 "reverse" -> Reverse
141 "reversed" -> Reverse
142 _ -> Regex
143
144
145
146
147 -- | Return a list of errors.
148 parse_errors :: IO [String]
149 parse_errors = do
150 argv <- getArgs
151 let (_, _, errors) = getOpt Permute options argv
152 return errors
153
154
155
156 -- | Is the help option set?
157 help_set :: IO Bool
158 help_set = do
159 opts <- parse_options
160 return (opt_help opts)
161
162
163 -- | Return our input function, getContents by default, or readFile if
164 -- the input file option was set.
165 input_function :: IO (IO String)
166 input_function = do
167 opts <- parse_options
168 return (opt_input opts)