@@ -5,7 +5,10 @@ module Checktestdata (
55
66 -- * Main functionality
77 ctdMain ,
8- runCTDFile ,
8+ ctdMainOpts ,
9+
10+ -- * Commandline parsers
11+ generalOpts ,
912
1013 -- * Primitives
1114 peekChar ,
@@ -31,38 +34,58 @@ import Checktestdata.Core
3134import Checktestdata.Derived
3235import Checktestdata.Options
3336
34- import System.Environment ( getArgs , getProgName )
35- import System.Exit ( exitFailure , exitSuccess )
37+ import System.Exit ( exitFailure )
3638import System.IO ( hPutStrLn , stderr )
3739
38- import qualified Data.ByteString.Char8 as BS
40+ import Control.Monad ( when )
41+
42+ import Options.Applicative
43+ import Data.Semigroup ((<>) )
3944
40- -- | Run a checktestdata script on a file
41- runCTDFile :: Options -> CTD a -> FilePath -> IO (Either String a )
42- runCTDFile opts sc fp = do
43- f <- BS. readFile fp
44- return $ runCTD opts sc f
45+ import qualified Data.ByteString.Char8 as BS
4546
4647-- | Main function that reads the commandline arguments
4748-- and takes either a filename or reads from stdin.
4849ctdMain :: CTD a -> IO ()
4950ctdMain sc = do
50- args <- getArgs
51- -- todo: add -w options to commandline arguments
52- bs <- case args of
53- [] -> BS. getContents
54- [" -" ] -> BS. getContents
55- [fp] -> BS. readFile fp
56- _ -> do
57- nm <- getProgName
58- putStrLn $ " Usage:"
59- putStrLn $ " " ++ nm ++ " data.in"
60- putStrLn $ " " ++ nm ++ " < data.in"
61- exitFailure
62- case runCTD defaultOptions sc bs of
51+ opts <- execParser $ info (generalOpts <**> helper)
52+ ( fullDesc
53+ <> progDesc " Check the data for testdata.in"
54+ <> header " checktestdata" )
55+ ctdMainOpts opts sc
56+
57+ -- | Main function that reads the input file given in the options.
58+ ctdMainOpts :: Options -> CTD a -> IO ()
59+ ctdMainOpts opts sc = do
60+ bs <- case input_file opts of
61+ Nothing -> BS. getContents
62+ Just " -" -> BS. getContents
63+ Just fp -> BS. readFile fp
64+ case runCTD opts sc bs of
6365 Left err -> do
64- hPutStrLn stderr err
66+ when (not $ quiet opts) $
67+ hPutStrLn stderr err
6568 exitFailure
6669 Right _ -> do
67- putStrLn " Testdata OK"
68- exitSuccess
70+ when (not $ quiet opts) $
71+ putStrLn " Testdata OK"
72+
73+
74+ --------------------------------------------------------------------------------
75+ -- Command line option parsing
76+ --------------------------------------------------------------------------------
77+
78+ -- | Parser for the general commandline options.
79+ generalOpts :: Parser Options
80+ generalOpts = Options
81+ <$> switch
82+ ( long " whitespace-ok"
83+ <> short ' w'
84+ <> help " whitespace changes are accepted, including heading and trailing whitespace, but not newlines; be careful: extra whitespace matches greedily!" )
85+ <*> switch
86+ ( long " quiet"
87+ <> short ' q'
88+ <> help " don't display testdata error messages: test exitcode" )
89+ <*> optional (argument str (metavar " testdata.in" ))
90+
91+
0 commit comments