- -- First, check for any errors that occurred while parsing
- -- the command line options.
- errors <- CommandLine.parse_errors
- if not (null errors)
- then do
- hPutStrLn stderr (concat errors)
- putStrLn CommandLine.help_text
- exitWith (ExitFailure exit_args_parse_failed)
- else do -- Nothing
-
- -- Next, check to see if the 'help' option was passed to the
- -- program. If it was, display the help, and exit successfully.
- help_opt_set <- CommandLine.help_set
- if help_opt_set
- then do
- putStrLn CommandLine.help_text
- exitWith ExitSuccess
- else do -- Nothing
-
- -- The input function we receive here should know what to read.
- inputfunc <- (CommandLine.input_function)
- input <- inputfunc
-
- let cidr_strings = lines input
- let cidrs = map cidr_from_string cidr_strings
-
- if (any isNothing cidrs)
- then do
- putStrLn "Error: not valid CIDR notation."
- exitWith (ExitFailure exit_invalid_cidr)
- else do -- Nothing
+ args <- get_args
+
+ -- This reads stdin.
+ input <- getContents
+
+ let cidr_strings = splitWs input
+ let cidrs = map readMaybe cidr_strings
+
+ when (any isNothing cidrs) $ do
+ hPutStrLn stderr "ERROR: not valid CIDR notation:"
+
+ -- Output the bad lines, safely.
+ let pairs = zip cidr_strings cidrs
+ let print_pair (x, Nothing) = hPutStrLn stderr (" * " ++ x)
+ print_pair (_, _) = return ()
+
+ mapM_ print_pair pairs
+ exitWith (ExitFailure exit_invalid_cidr)