]> gitweb.michael.orlitzky.com - hath.git/blob - src/CommandLine.hs
Add a new --normalize command-line flag to normalize CIDR output.
[hath.git] / src / CommandLine.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2
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.
6 --
7 -- Which is why we're allowed all of this unsafe voodoo.
8 --
9 module CommandLine (
10 Args(..),
11 get_args
12 )
13 where
14
15 import System.Console.CmdArgs (
16 Ann,
17 Annotate( (:=) ),
18 Data,
19 (+=),
20 auto,
21 cmdArgs_,
22 def,
23 details,
24 explicit,
25 groupname,
26 help,
27 helpArg,
28 modes_,
29 name,
30 program,
31 record,
32 summary,
33 versionArg )
34
35 -- Get the version from Cabal.
36 import Paths_hath (version)
37 import Data.Version (showVersion)
38
39 -- | The name of our program.
40 program_name :: String
41 program_name = "hath"
42
43 -- | A brief summary; displays the program name and version.
44 my_summary :: String
45 my_summary = program_name ++ "-" ++ (showVersion version)
46
47 barriers_help :: String
48 barriers_help =
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'"
51
52 normalize_help :: String
53 normalize_help =
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'"
56
57
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.
61 --
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.
65 --
66 data Args =
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 }
72 deriving (Data, Show)
73
74 -- | Description of the 'Regexed' mode.
75 regexed_description :: String
76 regexed_description =
77 "Compute a regular expression matching the input CIDRs."
78
79 -- | Description of the 'Reduced' mode.
80 reduced_description :: String
81 reduced_description =
82 "Combine any redundant/adjacent CIDR blocks into one."
83
84 -- | Description of the 'Duped' mode.
85 duped_description :: String
86 duped_description = "Display what would be removed by 'reduced'."
87
88 -- | Description of the 'Diffed' mode.
89 diffed_description :: String
90 diffed_description =
91 "Display both additions and deletions in a diff-like format."
92
93 -- | Description of the 'Listed' mode.
94 listed_description :: String
95 listed_description =
96 "Enumerate the IP addresses contained within the input CIDRs."
97
98 -- | We use explicit annotation here because if we use the magic
99 -- annotation, we have to duplicate the same argument definitions six
100 -- times.
101 --
102 arg_spec :: Annotate Ann
103 arg_spec =
104 modes_ [regexed += auto, reduced, duped, diffed, listed]
105 += program program_name
106 += summary my_summary
107 += helpArg [explicit,
108 name "help",
109 name "h",
110 groupname "Common flags"]
111 += versionArg [explicit,
112 name "version",
113 name "v",
114 groupname "Common flags"]
115 where
116 make_mode :: (Bool -> Bool -> Args) -> String -> (Annotate Ann)
117 make_mode ctor desc =
118 record (ctor def def)
119 [ barriers := def
120 += groupname "Common flags"
121 += help barriers_help,
122 normalize := def
123 += groupname "Common flags"
124 += help normalize_help ]
125 += details [" " ++ desc]
126
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
133 -- | This is the public interface; i.e. what main() should use to get
134 -- the command-line arguments.
135 get_args :: IO Args
136 get_args = cmdArgs_ arg_spec