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.
14 import Data.Char(toLower)
15 import System.Console.GetOpt
16 import System.Environment (getArgs)
20 lowercase :: String -> String
21 lowercase = map toLower
24 -- The application currently has four 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. Dupe will show you what would be removed by Reduce, and
28 -- Diff will show both additions and deletions in a diff-like format.
29 data Mode = Regex | Reduce | Dupe | Diff
32 -- A record containing values for all available options.
33 data Options = Options { opt_help :: Bool,
34 opt_input :: IO String }
37 -- This constructs an instance of Options, with each of its members
38 -- set to default values.
39 default_options :: Options
40 default_options = Options { opt_help = False,
41 opt_input = getContents }
44 -- The options list that we construct associates a function with each
45 -- option. This function is responsible for updating an Options record
46 -- with the appropriate value.
48 -- For more information and an example of this idiom, see,
50 -- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
52 options :: [OptDescr (Options -> IO Options)]
54 [ Option ['h'][] (NoArg set_help) "Prints this help message.",
55 Option ['i'][] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
57 -- Takes an Options as an argument, and sets its opt_help member to
59 set_help :: Options -> IO Options
61 return opts { opt_help = True }
64 -- If the input file option is set, this function will update the
65 -- passed Options record with a new function for opt_input. The
66 -- default opt_input is to read from stdin, but if this option is set,
67 -- we replace that with readFile.
68 set_input :: String -> Options -> IO Options
69 set_input arg opts = do
70 return opts { opt_input = readFile arg }
75 usage = "Usage: hath [regexed|reduced|duped|diffed] [-h] [-i FILE]"
78 -- The usage header, and all available flags (as generated by GetOpt)
80 help_text = usageInfo usage options
83 -- Return a list of options.
84 parse_options :: IO Options
87 let (actions, _, _) = getOpt Permute options argv
89 -- This will execute each of the functions contained in our options
90 -- list, one after another, on a default_options record. The end
91 -- result should be an Options instance with all of its members set
93 opts <- foldl (>>=) (return default_options) actions
98 -- Return the mode if one was given.
102 let (_, non_options, _) = getOpt Permute options argv
103 if (null non_options)
108 -- Some non-option was given, but were any of them modes?
109 case (lowercase (non_options !! 0)) of
110 "regex" -> return Regex
111 "regexed" -> return Regex
112 "reduce" -> return Reduce
113 "reduced" -> return Reduce
114 "dupe" -> return Dupe
115 "duped" -> return Dupe
116 "diff" -> return Diff
117 "diffed" -> return Diff
123 -- Return a list of errors.
124 parse_errors :: IO [String]
127 let (_, _, errors) = getOpt Permute options argv
132 -- Is the help option set?
135 opts <- parse_options
136 return (opt_help opts)
139 -- Return our input function, getContents by default, or readFile if
140 -- the input file option was set.
141 input_function :: IO (IO String)
143 opts <- parse_options
144 return (opt_input opts)