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)
19 -- | Lowercase an entire string.
20 lowercase :: String -> String
21 lowercase = map toLower
24 -- | The application currently has six modes. The default, Regex,
25 -- will compute a regular expression matching the input
28 -- Reduce, on the other hand, will combine any redundant/adjacent
29 -- CIDR blocks into one.
31 -- Dupe will show you what would be removed by Reduce.
33 -- Diff will show both additions and deletions in a diff-like
36 -- List will enumerate the IP addresses contained within the input
39 -- Reverse will perform a reverse DNS (PTR) lookup on each IP
40 -- address contained within the input CIDRs.
42 data Mode = Regex | Reduce | Dupe | Diff | List | Reverse
45 -- | A record containing values for all available options.
46 data Options = Options { opt_help :: Bool,
47 opt_input :: IO String }
50 -- | This constructs an instance of Options, with each of its members
51 -- set to default values.
52 default_options :: Options
53 default_options = Options { opt_help = False,
54 opt_input = getContents }
57 -- | The options list that we construct associates a function with
58 -- each option. This function is responsible for updating an Options
59 -- record with the appropriate value.
61 -- For more information and an example of this idiom, see,
63 -- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
65 options :: [OptDescr (Options -> IO Options)]
67 [ Option "h" ["help"] (NoArg set_help) "Prints this help message.",
68 Option "i" ["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ]
70 -- | Takes an Options as an argument, and sets its opt_help member to
72 set_help :: Options -> IO Options
74 return opts { opt_help = True }
77 -- | If the input file option is set, this function will update the
78 -- passed Options record with a new function for opt_input. The
79 -- default opt_input is to read from stdin, but if this option is
80 -- set, we replace that with readFile.
81 set_input :: String -> Options -> IO Options
83 return opts { opt_input = readFile arg }
86 -- | The usage header.
90 "[regexed|reduced|duped|diffed|listed|reversed] " ++
96 -- | The usage header, and all available flags (as generated by GetOpt).
98 help_text = usageInfo usage options
101 -- | Return a list of options.
102 parse_options :: IO Options
105 let (actions, _, _) = getOpt Permute options argv
107 -- This will execute each of the functions contained in our options
108 -- list, one after another, on a default_options record. The end
109 -- result should be an Options instance with all of its members set
111 foldl (>>=) (return default_options) actions
115 -- | Return the mode if one was given.
116 parse_mode :: IO Mode
119 let (_, non_options, _) = getOpt Permute options argv
120 return $ case non_options of
123 -- Some non-option was given, but were any of them modes?
125 case (lowercase x) of
137 "reversed" -> Reverse
143 -- | Return a list of errors.
144 parse_errors :: IO [String]
147 let (_, _, errors) = getOpt Permute options argv
152 -- | Is the help option set?
155 opts <- parse_options
156 return (opt_help opts)
159 -- | Return our input function, getContents by default, or readFile if
160 -- the input file option was set.
161 input_function :: IO (IO String)
163 opts <- parse_options
164 return (opt_input opts)