X-Git-Url: http://gitweb.michael.orlitzky.com/?p=hath.git;a=blobdiff_plain;f=src%2FCommandLine.hs;h=789f76c0addc7fa1281c9fd2ad711d9ac6fc073b;hp=31ca768cfaa043b872168313bc5153548bc37a5f;hb=a2fe46950a637e64fb5056fce091bf398b983a79;hpb=6c671cd410b81257514e1be76ca3f252ea69ccdb diff --git a/src/CommandLine.hs b/src/CommandLine.hs index 31ca768..789f76c 100644 --- a/src/CommandLine.hs +++ b/src/CommandLine.hs @@ -1,141 +1,137 @@ --- The CommandLine module handles parsing of the command-line options. --- It should more or less be a black box, providing Main with only the --- information it requires. +{-# LANGUAGE DeriveDataTypeable #-} -module CommandLine -( help_set, - help_text, - input_function, - 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 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 - - --- 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 -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 CommandLine module handles parsing of the command-line +-- options. It should more or less be a black box, providing Main +-- with only the information it requires. -- --- For more information and an example of this idiom, see, +-- Which is why we're allowed all of this unsafe voodoo. -- --- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt +module CommandLine ( + Args(..), + get_args +) +where + +import System.Console.CmdArgs ( + Ann, + Annotate( (:=) ), + Data, + Typeable, + (+=), + auto, + cmdArgs_, + def, + details, + explicit, + groupname, + help, + helpArg, + modes_, + name, + program, + record, + summary, + versionArg ) + +-- Get the version from Cabal. +import Paths_hath (version) +import Data.Version (showVersion) + +-- | The name of our program. +program_name :: String +program_name = "hath" + +-- | A brief summary; displays the program name and version. +my_summary :: String +my_summary = program_name ++ "-" ++ (showVersion version) + +barriers_help :: String +barriers_help = + "(regexed mode) place barriers in front/back of the regex " ++ + "to prevent e.g. '127.0.0.1' from matching '127.0.0.100'" + + +-- | The Args type represents the possible command-line options. The +-- duplication here seems necessary; CmdArgs' magic requires us to +-- define some things explicitly. -- -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." ] - --- Takes an Options as an argument, and sets its opt_help member to --- True. -set_help :: Options -> IO 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, --- we replace that with readFile. -set_input :: String -> Options -> IO Options -set_input arg opts = do - return opts { opt_input = readFile arg } - - --- The usage header -usage :: String -usage = "Usage: hath [regexed|reduced|duplicated] [-h] [-i FILE]" - - --- The usage header, and all available flags (as generated by GetOpt) -help_text :: String -help_text = usageInfo usage options - - --- Return a list of options. -parse_options :: IO Options -parse_options = do - argv <- getArgs - 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 - -- result should be an Options instance with all of its members set - -- correctly. - opts <- foldl (>>=) (return default_options) actions - - return opts - - --- 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. -parse_errors :: IO [String] -parse_errors = do - 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 - return (opt_help opts) - - --- 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 - return (opt_input opts) +-- The application currently has six modes (if this number is wrong, +-- it means I forgot to update the comment!), all of which take the +-- same options and arguments. +-- +data Args = + Regexed { barriers :: Bool } | + Reduced { barriers :: Bool } | + Duped { barriers :: Bool } | + Diffed { barriers :: Bool } | + Listed { barriers :: Bool } | + Reversed { barriers :: Bool } + deriving (Data, Show, Typeable) + +-- | Description of the 'Regexed' mode. +regexed_description :: String +regexed_description = + "Compute a regular expression matching the input CIDRs." + +-- | Description of the 'Reduced' mode. +reduced_description :: String +reduced_description = + "Combine any redundant/adjacent CIDR blocks into one." + +-- | Description of the 'Duped' mode. +duped_description :: String +duped_description = "Display what would be removed by 'reduced'." + +-- | Description of the 'Diffed' mode. +diffed_description :: String +diffed_description = + "Display both additions and deletions in a diff-like format." + +-- | Description of the 'Listed' mode. +listed_description :: String +listed_description = + "Enumerate the IP addresses contained within the input CIDRs." + +-- | Description of the 'Reversed' mode. +reversed_description :: String +reversed_description = + "Perform a reverse DNS (PTR) lookup on each IP address " ++ + "contained within the input CIDRs." + + +-- | We use explicit annotation here because if we use the magic +-- annotation, we have to duplicate the same argument definitions six +-- times. +-- +arg_spec :: Annotate Ann +arg_spec = + modes_ [regexed += auto, reduced, duped, diffed, listed, reversed] + += program program_name + += summary my_summary + += helpArg [explicit, + name "help", + name "h", + groupname "Common flags"] + += versionArg [explicit, + name "version", + name "v", + groupname "Common flags"] + where + make_mode :: (Bool -> Args) -> String -> (Annotate Ann) + make_mode ctor desc = + record (ctor def) [ barriers := def + += groupname "Common flags" + += help barriers_help ] + += details [" " ++ desc] + + regexed = make_mode Regexed regexed_description + reduced = make_mode Reduced reduced_description + duped = make_mode Duped duped_description + diffed = make_mode Diffed diffed_description + listed = make_mode Listed listed_description + reversed = make_mode Reversed reversed_description + +-- | This is the public interface; i.e. what main() should use to get +-- the command-line arguments. +get_args :: IO Args +get_args = cmdArgs_ arg_spec