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