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 (
35 -- Get the version from Cabal.
36 import Paths_hath (version)
37 import Data.Version (showVersion)
39 -- | The name of our program.
40 program_name :: String
43 -- | A brief summary; displays the program name and version.
45 my_summary = program_name ++ "-" ++ (showVersion version)
47 barriers_help :: String
49 "(regexed mode) place barriers in front/back of the regex " ++
50 "to prevent e.g. '127.0.0.1' from matching '127.0.0.100'"
52 normalize_help :: String
54 "(reduced mode) normalize the output CIDRs, replacing any " ++
55 "masked bits by zeros; e.g. '127.0.0.1/8' -> '127.0.0.0/8'"
60 "(reduced mode) sort the output CIDRs by their octets"
63 -- | The Args type represents the possible command-line options. The
64 -- duplication here seems necessary; CmdArgs' magic requires us to
65 -- define some things explicitly.
67 -- The application currently has five modes (if this number is wrong,
68 -- it means I forgot to update the comment!), all of which take the
69 -- same options and arguments.
72 Regexed { barriers :: Bool, normalize :: Bool, sort :: Bool } |
73 Reduced { barriers :: Bool, normalize :: Bool, sort :: Bool } |
74 Duped { barriers :: Bool, normalize :: Bool, sort :: Bool } |
75 Diffed { barriers :: Bool, normalize :: Bool, sort :: Bool } |
76 Listed { barriers :: Bool, normalize :: Bool, sort :: Bool }
79 -- | Description of the 'Regexed' mode.
80 regexed_description :: String
82 "Compute a regular expression matching the input CIDRs."
84 -- | Description of the 'Reduced' mode.
85 reduced_description :: String
87 "Combine any redundant/adjacent CIDR blocks into one."
89 -- | Description of the 'Duped' mode.
90 duped_description :: String
91 duped_description = "Display what would be removed by 'reduced'."
93 -- | Description of the 'Diffed' mode.
94 diffed_description :: String
96 "Display both additions and deletions in a diff-like format."
98 -- | Description of the 'Listed' mode.
99 listed_description :: String
101 "Enumerate the IP addresses contained within the input CIDRs."
103 -- | We use explicit annotation here because if we use the magic
104 -- annotation, we have to duplicate the same argument definitions six
107 arg_spec :: Annotate Ann
109 modes_ [regexed += auto, reduced, duped, diffed, listed]
110 += program program_name
111 += summary my_summary
112 += helpArg [explicit,
115 groupname "Common flags"]
116 += versionArg [explicit,
119 groupname "Common flags"]
121 make_mode :: (Bool -> Bool -> Bool -> Args) -> String -> (Annotate Ann)
122 make_mode ctor desc =
123 record (ctor def def def)
125 += groupname "Common flags"
126 += help barriers_help,
128 += groupname "Common flags"
129 += help normalize_help,
131 += groupname "Common flags"
133 += details [" " ++ desc]
135 regexed = make_mode Regexed regexed_description
136 reduced = make_mode Reduced reduced_description
137 duped = make_mode Duped duped_description
138 diffed = make_mode Diffed diffed_description
139 listed = make_mode Listed listed_description
141 -- | This is the public interface; i.e. what main() should use to get
142 -- the command-line arguments.
144 get_args = cmdArgs_ arg_spec