]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/CommandLine.hs
Bump the version number to 0.0.4 in hath.cabal.
[hath.git] / src / CommandLine.hs
index 31ca768cfaa043b872168313bc5153548bc37a5f..8ed8597fe25d62e7390882a460f0787a6c90e10a 100644 (file)
@@ -16,70 +16,89 @@ import System.Console.GetOpt
 import System.Environment (getArgs)
 
 
--- Dark magic.
+-- | Lowercase an entire string.
 lowercase :: String -> String
 lowercase = map toLower
 
 
--- The application currently has three 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.
-data Mode = Regex | Reduce | Dupe
+-- | The application currently has six 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.
+--
+--   Diff will show both additions and deletions in a diff-like
+--   format.
+--
+--   List will enumerate the IP addresses contained within the input
+--   CIDRs.
+--
+--   Reverse will perform a reverse DNS (PTR) lookup on each IP
+--   address contained within the input CIDRs.
+--
+data Mode = Regex | Reduce | Dupe | Diff | List | Reverse
 
 
--- A record containing values for all available options.
+-- 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.
+-- This constructs an instance of Options, with each of its members
+--   set to default values.
 default_options :: Options
 default_options = Options { opt_help = False,
                             opt_input = getContents }
 
 
--- The options list that we construct associates a function with each
--- option. This function is responsible for updating an Options record
--- with the appropriate value.
+-- | The options list that we construct associates a function with
+--   each option. This function is responsible for updating an Options
+--   record with the appropriate value.
 --
--- For more information and an example of this idiom, see,
+--   For more information and an example of this idiom, see,
 --
--- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
+--   http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
 --
 options :: [OptDescr (Options -> IO Options)]
 options =
-  [ Option ['h'][] (NoArg set_help) "Prints this help message.",
-    Option ['i'][] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
+  [ Option "h" ["help"] (NoArg set_help) "Prints this help message.",
+    Option "i" ["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
 
--- Takes an Options as an argument, and sets its opt_help member to
--- True.
+-- Takes an Options as an argument, and sets its opt_help member to
+--   True.
 set_help :: Options -> IO Options
-set_help opts = do
+set_help opts =
   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,
--- we replace that with readFile.
+-- 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, we replace that with readFile.
 set_input :: String -> Options -> IO Options
-set_input arg opts = do
+set_input arg opts =
   return opts { opt_input = readFile arg }
 
 
--- The usage header
+-- | The usage header.
 usage :: String
-usage = "Usage: hath [regexed|reduced|duplicated] [-h] [-i FILE]"
+usage =
+  "Usage: hath " ++
+  "[regexed|reduced|duped|diffed|listed|reversed] " ++
+  "[-h] " ++
+  "[-i FILE] " ++
+  "<input>"
 
 
--- The usage header, and all available flags (as generated by GetOpt)
+-- | The usage header, and all available flags (as generated by GetOpt).
 help_text :: String
 help_text = usageInfo usage options
 
 
--- Return a list of options.
+-- Return a list of options.
 parse_options :: IO Options
 parse_options = do
   argv <- getArgs
@@ -89,35 +108,39 @@ parse_options = do
   -- list, one after another, on a default_options record. The end
   -- result should be an Options instance with all of its members set
   -- correctly.
-  opts <- foldl (>>=) (return default_options) actions
+  foldl (>>=) (return default_options) actions
 
-  return opts
 
 
--- Return the mode if one was given.
+-- 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
-        "duplicated" -> return Dupe
-        _         -> return Regex
-
-
-
-
--- Return a list of errors.
+  return $ case non_options of
+    -- Default
+    []    -> Regex
+    -- Some non-option was given, but were any of them modes?
+    (x:_) ->
+      case (lowercase x) of
+        "regex"    -> Regex
+        "regexed"  -> Regex
+        "reduce"   -> Reduce
+        "reduced"  -> Reduce
+        "dupe"     -> Dupe
+        "duped"    -> Dupe
+        "diff"     -> Diff
+        "diffed"   -> Diff
+        "list"     -> List
+        "listed"   -> List
+        "reverse"  -> Reverse
+        "reversed" -> Reverse
+        _          -> Regex
+
+
+
+
+-- | Return a list of errors.
 parse_errors :: IO [String]
 parse_errors = do
   argv <- getArgs
@@ -126,15 +149,15 @@ parse_errors = do
 
 
 
--- Is the help option set?
+-- Is the help option set?
 help_set :: IO Bool
 help_set = do
     opts <- parse_options
     return (opt_help opts)
 
 
--- Return our input function, getContents by default, or readFile if
--- the input file option was set.
+-- Return our input function, getContents by default, or readFile if
+--   the input file option was set.
 input_function :: IO (IO String)
 input_function = do
     opts <- parse_options