]> gitweb.michael.orlitzky.com - hath.git/blob - src/CommandLine.hs
f15a786c9089de96d7655bac2270dc1270aae26c
[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 -- Dark magic.
20 lowercase :: String -> String
21 lowercase = map toLower
22
23
24 -- The application currently has two modes. The default, Regex, will
25 -- compute a regular expression matching the input CIDRs. Reduce, on
26 -- the other hand, will combine any redundant/adjacent CIDR blocks
27 -- into one.
28 data Mode = Regex | Reduce
29
30
31 -- A record containing values for all available options.
32 data Options = Options { opt_help :: Bool,
33 opt_input :: IO String }
34
35
36 -- This constructs an instance of Options, with each of its members
37 -- set to default values.
38 default_options :: Options
39 default_options = Options { opt_help = False,
40 opt_input = getContents }
41
42
43 -- The options list that we construct associates a function with each
44 -- option. This function is responsible for updating an Options record
45 -- with the appropriate value.
46 --
47 -- For more information and an example of this idiom, see,
48 --
49 -- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
50 --
51 options :: [OptDescr (Options -> IO Options)]
52 options =
53 [ Option ['h'][] (NoArg set_help) "Prints this help message.",
54 Option ['i'][] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
55
56 -- Takes an Options as an argument, and sets its opt_help member to
57 -- True.
58 set_help :: Options -> IO Options
59 set_help opts = do
60 return opts { opt_help = True }
61
62
63 -- If the input file option is set, this function will update the
64 -- passed Options record with a new function for opt_input. The
65 -- default opt_input is to read from stdin, but if this option is set,
66 -- we replace that with readFile.
67 set_input :: String -> Options -> IO Options
68 set_input arg opts = do
69 return opts { opt_input = readFile arg }
70
71
72 -- The usage header
73 usage :: String
74 usage = "Usage: hath [regexed|reduced] [-h] [-i FILE]"
75
76
77 -- The usage header, and all available flags (as generated by GetOpt)
78 help_text :: String
79 help_text = usageInfo usage options
80
81
82 -- Return a list of options.
83 parse_options :: IO Options
84 parse_options = do
85 argv <- getArgs
86 let (actions, _, _) = getOpt Permute options argv
87
88 -- This will execute each of the functions contained in our options
89 -- list, one after another, on a default_options record. The end
90 -- result should be an Options instance with all of its members set
91 -- correctly.
92 opts <- foldl (>>=) (return default_options) actions
93
94 return opts
95
96
97 -- Return the mode if one was given.
98 parse_mode :: IO Mode
99 parse_mode = do
100 argv <- getArgs
101 let (_, non_options, _) = getOpt Permute options argv
102 if (null non_options)
103 then do
104 -- Default
105 return Regex
106 else do
107 -- Some non-option was given, but were any of them modes?
108 case (lowercase (non_options !! 0)) of
109 "regex" -> return Regex
110 "regexed" -> return Regex
111 "reduce" -> return Reduce
112 "reduced" -> return Reduce
113 _ -> return Regex
114
115
116
117
118 -- Return a list of errors.
119 parse_errors :: IO [String]
120 parse_errors = do
121 argv <- getArgs
122 let (_, _, errors) = getOpt Permute options argv
123 return errors
124
125
126
127 -- Is the help option set?
128 help_set :: IO Bool
129 help_set = do
130 opts <- parse_options
131 return (opt_help opts)
132
133
134 -- Return our input function, getContents by default, or readFile if
135 -- the input file option was set.
136 input_function :: IO (IO String)
137 input_function = do
138 opts <- parse_options
139 return (opt_input opts)