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