]> gitweb.michael.orlitzky.com - hath.git/blob - src/CommandLine.hs
d0cf6f4f76bd7cdf038afa0be41f6510615674eb
[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 deriving (Data, Show, Typeable)
69
70 -- | Description of the 'Regexed' mode.
71 regexed_description :: String
72 regexed_description =
73 "Compute a regular expression matching the input CIDRs."
74
75 -- | Description of the 'Reduced' mode.
76 reduced_description :: String
77 reduced_description =
78 "Combine any redundant/adjacent CIDR blocks into one."
79
80 -- | Description of the 'Duped' mode.
81 duped_description :: String
82 duped_description = "Display what would be removed by 'reduced'."
83
84 -- | Description of the 'Diffed' mode.
85 diffed_description :: String
86 diffed_description =
87 "Display both additions and deletions in a diff-like format."
88
89 -- | Description of the 'Listed' mode.
90 listed_description :: String
91 listed_description =
92 "Enumerate the IP addresses contained within the input CIDRs."
93
94 -- | We use explicit annotation here because if we use the magic
95 -- annotation, we have to duplicate the same argument definitions six
96 -- times.
97 --
98 arg_spec :: Annotate Ann
99 arg_spec =
100 modes_ [regexed += auto, reduced, duped, diffed, listed]
101 += program program_name
102 += summary my_summary
103 += helpArg [explicit,
104 name "help",
105 name "h",
106 groupname "Common flags"]
107 += versionArg [explicit,
108 name "version",
109 name "v",
110 groupname "Common flags"]
111 where
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]
118
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
124
125 -- | This is the public interface; i.e. what main() should use to get
126 -- the command-line arguments.
127 get_args :: IO Args
128 get_args = cmdArgs_ arg_spec