1 -- | The CommandLine module handles parsing of the command-line
2 -- options. It should more or less be a black box, providing Main
3 -- with only the information it requires.
13 import Data.Char (toLower)
14 import System.Console.GetOpt (
15 ArgDescr(NoArg, ReqArg),
20 import System.Environment (getArgs)
23 -- | Lowercase an entire string.
24 lowercase :: String -> String
25 lowercase = map toLower
28 -- | The application currently has six modes. The default, Regex,
29 -- will compute a regular expression matching the input
32 -- Reduce, on the other hand, will combine any redundant/adjacent
33 -- CIDR blocks into one.
35 -- Dupe will show you what would be removed by Reduce.
37 -- Diff will show both additions and deletions in a diff-like
40 -- List will enumerate the IP addresses contained within the input
43 -- Reverse will perform a reverse DNS (PTR) lookup on each IP
44 -- address contained within the input CIDRs.
46 data Mode = Regex | Reduce | Dupe | Diff | List | Reverse
49 -- | A record containing values for all available options.
50 data Options = Options { opt_help :: Bool,
51 opt_input :: IO String }
54 -- | This constructs an instance of Options, with each of its members
55 -- set to default values.
56 default_options :: Options
57 default_options = Options { opt_help = False,
58 opt_input = getContents }
61 -- | The options list that we construct associates a function with
62 -- each option. This function is responsible for updating an Options
63 -- record with the appropriate value.
65 -- For more information and an example of this idiom, see,
67 -- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
69 options :: [OptDescr (Options -> IO Options)]
71 [ Option "h" ["help"] (NoArg set_help) "Prints this help message.",
72 Option "i" ["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
74 -- | Takes an Options as an argument, and sets its opt_help member to
76 set_help :: Options -> IO Options
78 return opts { opt_help = True }
81 -- | If the input file option is set, this function will update the
82 -- passed Options record with a new function for opt_input. The
83 -- default opt_input is to read from stdin, but if this option is
84 -- set, we replace that with readFile.
85 set_input :: String -> Options -> IO Options
87 return opts { opt_input = readFile arg }
90 -- | The usage header.
94 "[regexed|reduced|duped|diffed|listed|reversed] " ++
100 -- | The usage header, and all available flags (as generated by GetOpt).
102 help_text = usageInfo usage options
105 -- | Return a list of options.
106 parse_options :: IO Options
109 let (actions, _, _) = getOpt Permute options argv
111 -- This will execute each of the functions contained in our options
112 -- list, one after another, on a default_options record. The end
113 -- result should be an Options instance with all of its members set
115 foldl (>>=) (return default_options) actions
119 -- | Return the mode if one was given.
120 parse_mode :: IO Mode
123 let (_, non_options, _) = getOpt Permute options argv
124 return $ case non_options of
127 -- Some non-option was given, but were any of them modes?
129 case (lowercase x) of
141 "reversed" -> Reverse
147 -- | Return a list of errors.
148 parse_errors :: IO [String]
151 let (_, _, errors) = getOpt Permute options argv
156 -- | Is the help option set?
159 opts <- parse_options
160 return (opt_help opts)
163 -- | Return our input function, getContents by default, or readFile if
164 -- the input file option was set.
165 input_function :: IO (IO String)
167 opts <- parse_options
168 return (opt_input opts)