( help_set,
help_text,
input_function,
- parse_errors
+ Mode(..),
+ parse_errors,
+ parse_mode
) where
+import Data.Char(toLower)
import System.Console.GetOpt
import System.Environment (getArgs)
+-- Dark magic.
+lowercase :: String -> String
+lowercase = map toLower
+
+
+-- The application currently has four modes. The default, Regex, will
+-- compute a regular expression matching the input CIDRs. Reduce, on
+-- the other hand, will combine any redundant/adjacent CIDR blocks
+-- into one. Dupe will show you what would be removed by Reduce, and
+-- Diff will show both additions and deletions in a diff-like format.
+data Mode = Regex | Reduce | Dupe | Diff
+
+
-- A record containing values for all available options.
data Options = Options { opt_help :: Bool,
opt_input :: IO String }
+
-- This constructs an instance of Options, with each of its members
-- set to default values.
default_options :: Options
set_help opts = do
return opts { opt_help = True }
+
-- If the input file option is set, this function will update the
-- passed Options record with a new function for opt_input. The
-- default opt_input is to read from stdin, but if this option is set,
set_input arg opts = do
return opts { opt_input = readFile arg }
+
-- The usage header
usage :: String
-usage = "Usage: hath [-h] [-i FILE]"
+usage = "Usage: hath [regexed|reduced|duped|diffed] [-h] [-i FILE]"
-- The usage header, and all available flags (as generated by GetOpt)
help_text = usageInfo usage options
--- Return a tuple of options and errors.
-parse_options :: IO (Options, [String])
+-- Return a list of options.
+parse_options :: IO Options
parse_options = do
argv <- getArgs
- let (actions, _, errors) = getOpt Permute options argv
+ let (actions, _, _) = getOpt Permute options argv
-- This will execute each of the functions contained in our options
-- list, one after another, on a default_options record. The end
-- correctly.
opts <- foldl (>>=) (return default_options) actions
- return (opts, errors)
+ return opts
--- Return just the errors from parse_options.
+-- Return the mode if one was given.
+parse_mode :: IO Mode
+parse_mode = do
+ argv <- getArgs
+ let (_, non_options, _) = getOpt Permute options argv
+ if (null non_options)
+ then do
+ -- Default
+ return Regex
+ else do
+ -- Some non-option was given, but were any of them modes?
+ case (lowercase (non_options !! 0)) of
+ "regex" -> return Regex
+ "regexed" -> return Regex
+ "reduce" -> return Reduce
+ "reduced" -> return Reduce
+ "dupe" -> return Dupe
+ "duped" -> return Dupe
+ "diff" -> return Diff
+ "diffed" -> return Diff
+ _ -> return Regex
+
+
+
+
+-- Return a list of errors.
parse_errors :: IO [String]
parse_errors = do
- (_, errors) <- parse_options
- return errors
+ argv <- getArgs
+ let (_, _, errors) = getOpt Permute options argv
+ return errors
+
-- Is the help option set?
help_set :: IO Bool
help_set = do
- (opts, _) <- parse_options
+ opts <- parse_options
return (opt_help opts)
-- the input file option was set.
input_function :: IO (IO String)
input_function = do
- (opts, _) <- parse_options
+ opts <- parse_options
return (opt_input opts)