1 {-# LANGUAGE DeriveDataTypeable #-}
3 -- | The CommandLine module handles parsing of the command-line
4 -- options. It should more or less be a black box, providing Main
5 -- with only the information it requires.
7 -- Which is why we're allowed all of this unsafe voodoo.
15 import System.Console.CmdArgs (
36 -- Get the version from Cabal.
37 import Paths_hath (version)
38 import Data.Version (showVersion)
40 -- | The name of our program.
41 program_name :: String
44 -- | A brief summary; displays the program name and version.
46 my_summary = program_name ++ "-" ++ (showVersion version)
48 barriers_help :: String
50 "(regexed mode) place barriers in front/back of the regex " ++
51 "to prevent e.g. '127.0.0.1' from matching '127.0.0.100'"
54 -- | The Args type represents the possible command-line options. The
55 -- duplication here seems necessary; CmdArgs' magic requires us to
56 -- define some things explicitly.
58 -- The application currently has six modes (if this number is wrong,
59 -- it means I forgot to update the comment!), all of which take the
60 -- same options and arguments.
63 Regexed { barriers :: Bool } |
64 Reduced { barriers :: Bool } |
65 Duped { barriers :: Bool } |
66 Diffed { barriers :: Bool } |
67 Listed { barriers :: Bool } |
68 Reversed { barriers :: Bool }
69 deriving (Data, Show, Typeable)
71 -- | Description of the 'Regexed' mode.
72 regexed_description :: String
74 "Compute a regular expression matching the input CIDRs."
76 -- | Description of the 'Reduced' mode.
77 reduced_description :: String
79 "Combine any redundant/adjacent CIDR blocks into one."
81 -- | Description of the 'Duped' mode.
82 duped_description :: String
83 duped_description = "Display what would be removed by 'reduced'."
85 -- | Description of the 'Diffed' mode.
86 diffed_description :: String
88 "Display both additions and deletions in a diff-like format."
90 -- | Description of the 'Listed' mode.
91 listed_description :: String
93 "Enumerate the IP addresses contained within the input CIDRs."
95 -- | Description of the 'Reversed' mode.
96 reversed_description :: String
97 reversed_description =
98 "Perform a reverse DNS (PTR) lookup on each IP address " ++
99 "contained within the input CIDRs."
102 -- | We use explicit annotation here because if we use the magic
103 -- annotation, we have to duplicate the same argument definitions six
106 arg_spec :: Annotate Ann
108 modes_ [regexed += auto, reduced, duped, diffed, listed, reversed]
109 += program program_name
110 += summary my_summary
111 += helpArg [explicit,
114 groupname "Common flags"]
115 += versionArg [explicit,
118 groupname "Common flags"]
120 make_mode :: (Bool -> Args) -> String -> (Annotate Ann)
121 make_mode ctor desc =
122 record (ctor def) [ barriers := def
123 += groupname "Common flags"
124 += help barriers_help ]
125 += details [" " ++ desc]
127 regexed = make_mode Regexed regexed_description
128 reduced = make_mode Reduced reduced_description
129 duped = make_mode Duped duped_description
130 diffed = make_mode Diffed diffed_description
131 listed = make_mode Listed listed_description
132 reversed = make_mode Reversed reversed_description
134 -- | This is the public interface; i.e. what main() should use to get
135 -- the command-line arguments.
137 get_args = cmdArgs_ arg_spec