]> gitweb.michael.orlitzky.com - hath.git/blob - src/CommandLine.hs
Rewrite command-line parsing to use cmdargs.
[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 Typeable,
20 (+=),
21 auto,
22 cmdArgs_,
23 def,
24 details,
25 explicit,
26 groupname,
27 help,
28 helpArg,
29 modes_,
30 name,
31 program,
32 record,
33 summary,
34 versionArg )
35
36 -- Get the version from Cabal.
37 import Paths_hath (version)
38 import Data.Version (showVersion)
39
40 -- | The name of our program.
41 program_name :: String
42 program_name = "hath"
43
44 -- | A brief summary; displays the program name and version.
45 my_summary :: String
46 my_summary = program_name ++ "-" ++ (showVersion version)
47
48 barriers_help :: String
49 barriers_help =
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'"
52
53
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.
57 --
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.
61 --
62 data Args =
63 Regexed { barriers :: Bool } |
64 Reduced { barriers :: Bool } |
65 Duped { barriers :: Bool } |
66 Diffed { barriers :: Bool } |
67 Listed { barriers :: Bool } |
68 Reversed { barriers :: Bool }
69 deriving (Data, Show, Typeable)
70
71 -- | Description of the 'Regexed' mode.
72 regexed_description :: String
73 regexed_description =
74 "Compute a regular expression matching the input CIDRs."
75
76 -- | Description of the 'Reduced' mode.
77 reduced_description :: String
78 reduced_description =
79 "Combine any redundant/adjacent CIDR blocks into one."
80
81 -- | Description of the 'Duped' mode.
82 duped_description :: String
83 duped_description = "Display what would be removed by 'reduced'."
84
85 -- | Description of the 'Diffed' mode.
86 diffed_description :: String
87 diffed_description =
88 "Display both additions and deletions in a diff-like format."
89
90 -- | Description of the 'Listed' mode.
91 listed_description :: String
92 listed_description =
93 "Enumerate the IP addresses contained within the input CIDRs."
94
95 -- | Description of the 'Reversed' mode.
96 reversed_description :: String
97 reversed_description =
98 "Perform a reverse DNS (PTR) lookup on each IP address " ++
99 "contained within the input CIDRs."
100
101
102 -- | We use explicit annotation here because if we use the magic
103 -- annotation, we have to duplicate the same argument definitions six
104 -- times.
105 --
106 arg_spec :: Annotate Ann
107 arg_spec =
108 modes_ [regexed += auto, reduced, duped, diffed, listed, reversed]
109 += program program_name
110 += summary my_summary
111 += helpArg [explicit,
112 name "help",
113 name "h",
114 groupname "Common flags"]
115 += versionArg [explicit,
116 name "version",
117 name "v",
118 groupname "Common flags"]
119 where
120 make_mode :: (Bool -> Args) -> String -> (Annotate Ann)
121 make_mode ctor desc =
122 record (ctor def) [ barriers := def
123 += groupname "Common flags"
124 += help barriers_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 reversed = make_mode Reversed reversed_description
133
134 -- | This is the public interface; i.e. what main() should use to get
135 -- the command-line arguments.
136 get_args :: IO Args
137 get_args = cmdArgs_ arg_spec