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