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 deriving (Data, Show, Typeable)
70 -- | Description of the 'Regexed' mode.
71 regexed_description :: String
73 "Compute a regular expression matching the input CIDRs."
75 -- | Description of the 'Reduced' mode.
76 reduced_description :: String
78 "Combine any redundant/adjacent CIDR blocks into one."
80 -- | Description of the 'Duped' mode.
81 duped_description :: String
82 duped_description = "Display what would be removed by 'reduced'."
84 -- | Description of the 'Diffed' mode.
85 diffed_description :: String
87 "Display both additions and deletions in a diff-like format."
89 -- | Description of the 'Listed' mode.
90 listed_description :: String
92 "Enumerate the IP addresses contained within the input CIDRs."
94 -- | We use explicit annotation here because if we use the magic
95 -- annotation, we have to duplicate the same argument definitions six
98 arg_spec :: Annotate Ann
100 modes_ [regexed += auto, reduced, duped, diffed, listed]
101 += program program_name
102 += summary my_summary
103 += helpArg [explicit,
106 groupname "Common flags"]
107 += versionArg [explicit,
110 groupname "Common flags"]
112 make_mode :: (Bool -> Args) -> String -> (Annotate Ann)
113 make_mode ctor desc =
114 record (ctor def) [ barriers := def
115 += groupname "Common flags"
116 += help barriers_help ]
117 += details [" " ++ desc]
119 regexed = make_mode Regexed regexed_description
120 reduced = make_mode Reduced reduced_description
121 duped = make_mode Duped duped_description
122 diffed = make_mode Diffed diffed_description
123 listed = make_mode Listed listed_description
125 -- | This is the public interface; i.e. what main() should use to get
126 -- the command-line arguments.
128 get_args = cmdArgs_ arg_spec