From 4bce4546cbd70c8aab8ca81441010e0527ebbb07 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 22 Nov 2013 18:53:35 -0500 Subject: [PATCH] Greatly simplify the CommandLine module. --- src/CommandLine.hs | 48 +++++++++++++++++----------------------------- src/Main.hs | 4 ++-- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/CommandLine.hs b/src/CommandLine.hs index 405ded8..ad60c9f 100644 --- a/src/CommandLine.hs +++ b/src/CommandLine.hs @@ -1,19 +1,27 @@ {-# LANGUAGE DeriveDataTypeable #-} -module CommandLine +module CommandLine ( + Args(..), + get_args ) where -import System.Console.CmdArgs -import System.Console.CmdArgs.Explicit (process) -import System.Environment (getArgs, withArgs) -import System.Exit (ExitCode(..), exitWith) -import System.IO (hPutStrLn, stderr) +import System.Console.CmdArgs ( + Data, + Typeable, + (&=), + cmdArgs, + def, + details, + help, + program, + summary, + typFile) + -- Get the version from Cabal. import Paths_email_validator (version) import Data.Version (showVersion) -import ExitCodes -- We optionally accept input/output files to use instead of -- stdin/stdout. @@ -48,9 +56,8 @@ rfc5322_help :: String rfc5322_help = "Validate according to RFC 5322 (incredibly lenient)." -arg_spec :: Mode (CmdArgs Args) +arg_spec :: Args arg_spec = - cmdArgsMode $ Args { accept_a = def &= help accept_a_help, input_file = def &= typFile &= help input_file_help, output_file = def &= typFile &= help output_file_help, @@ -59,26 +66,7 @@ arg_spec = &= summary my_summary &= details [description] -show_help :: IO (CmdArgs Args) -show_help = withArgs ["--help"] parse_args - - - -parse_args :: IO (CmdArgs Args) -parse_args = do - x <- getArgs - let y = process arg_spec x - case y of - Right result -> return result - Left err -> do - hPutStrLn stderr err - exitWith (ExitFailure exit_args_parse_failed) --- | Really get the command-line arguments. This calls 'parse_args' --- first to replace the default "wrong number of arguments" error, --- and then runs 'cmdArgsApply' on the result to do what the --- 'cmdArgs' function usually does. -apply_args :: IO Args -apply_args = - parse_args >>= cmdArgsApply +get_args :: IO Args +get_args = cmdArgs arg_spec diff --git a/src/Main.hs b/src/Main.hs index 0e7c30f..551dae7 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -34,7 +34,7 @@ import System.IO ( stdout) -import CommandLine (Args(..), apply_args) +import CommandLine (Args(..), get_args) import EmailAddress import ExitCodes (exit_input_file_doesnt_exist) @@ -103,7 +103,7 @@ validate resolver accept_a rfc5322 address = do main :: IO () main = do - Args{..} <- apply_args + Args{..} <- get_args -- Get the input from either stdin, or the file given on the command -- line. -- 2.44.2