]> gitweb.michael.orlitzky.com - email-validator.git/blob - src/CommandLine.hs
Add command-line processing.
[email-validator.git] / src / CommandLine.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2
3 module CommandLine
4 where
5
6 import System.Console.CmdArgs
7 import System.Console.CmdArgs.Explicit (process)
8 import System.Environment (getArgs, withArgs)
9 import System.Exit (ExitCode(..), exitWith)
10 import System.IO (hPutStrLn, stderr)
11
12 -- Get the version from Cabal.
13 import Paths_email_validator (version)
14 import Data.Version (showVersion)
15
16 import ExitCodes
17
18 -- We optionally accept input/output files to use instead of
19 -- stdin/stdout.
20 data Args = Args { input_file :: Maybe FilePath,
21 output_file :: Maybe FilePath }
22 deriving (Show, Data, Typeable)
23
24 description :: String
25 description = "Perform naive validation of email addresses."
26
27 program_name :: String
28 program_name = "email-validator"
29
30 my_summary :: String
31 my_summary = program_name ++ "-" ++ (showVersion version)
32
33 input_file_help :: String
34 input_file_help =
35 "Path to the input file (default: stdin), one email address per line"
36
37 output_file_help :: String
38 output_file_help =
39 "Path to the output file (default: stdout)"
40
41 arg_spec :: Mode (CmdArgs Args)
42 arg_spec =
43 cmdArgsMode $
44 Args { input_file = def &= typFile &= help input_file_help,
45 output_file = def &= typFile &= help output_file_help }
46 &= program program_name
47 &= summary my_summary
48 &= details [description]
49
50 show_help :: IO (CmdArgs Args)
51 show_help = withArgs ["--help"] parse_args
52
53
54
55 parse_args :: IO (CmdArgs Args)
56 parse_args = do
57 x <- getArgs
58 let y = process arg_spec x
59 case y of
60 Right result -> return result
61 Left err -> do
62 hPutStrLn stderr err
63 exitWith (ExitFailure exit_args_parse_failed)
64
65
66 -- | Really get the command-line arguments. This calls 'parse_args'
67 -- first to replace the default "wrong number of arguments" error,
68 -- and then runs 'cmdArgsApply' on the result to do what the
69 -- 'cmdArgs' function usually does.
70 apply_args :: IO Args
71 apply_args =
72 parse_args >>= cmdArgsApply