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'"
58 -- | The Args type represents the possible command-line options. The
59 -- duplication here seems necessary; CmdArgs' magic requires us to
60 -- define some things explicitly.
62 -- The application currently has five modes (if this number is wrong,
63 -- it means I forgot to update the comment!), all of which take the
64 -- same options and arguments.
67 Regexed { barriers :: Bool, normalize :: Bool } |
68 Reduced { barriers :: Bool, normalize :: Bool } |
69 Duped { barriers :: Bool, normalize :: Bool } |
70 Diffed { barriers :: Bool, normalize :: Bool } |
71 Listed { barriers :: Bool, normalize :: Bool }
74 -- | Description of the 'Regexed' mode.
75 regexed_description :: String
77 "Compute a regular expression matching the input CIDRs."
79 -- | Description of the 'Reduced' mode.
80 reduced_description :: String
82 "Combine any redundant/adjacent CIDR blocks into one."
84 -- | Description of the 'Duped' mode.
85 duped_description :: String
86 duped_description = "Display what would be removed by 'reduced'."
88 -- | Description of the 'Diffed' mode.
89 diffed_description :: String
91 "Display both additions and deletions in a diff-like format."
93 -- | Description of the 'Listed' mode.
94 listed_description :: String
96 "Enumerate the IP addresses contained within the input CIDRs."
98 -- | We use explicit annotation here because if we use the magic
99 -- annotation, we have to duplicate the same argument definitions six
102 arg_spec :: Annotate Ann
104 modes_ [regexed += auto, reduced, duped, diffed, listed]
105 += program program_name
106 += summary my_summary
107 += helpArg [explicit,
110 groupname "Common flags"]
111 += versionArg [explicit,
114 groupname "Common flags"]
116 make_mode :: (Bool -> Bool -> Args) -> String -> (Annotate Ann)
117 make_mode ctor desc =
118 record (ctor def def)
120 += groupname "Common flags"
121 += help barriers_help,
123 += groupname "Common flags"
124 += help normalize_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
133 -- | This is the public interface; i.e. what main() should use to get
134 -- the command-line arguments.
136 get_args = cmdArgs_ arg_spec