Skip to content

Commit efde894

Browse files
committed
[haskell_edsl] Proper commandline parsing and -q option
1 parent 00da33c commit efde894

File tree

5 files changed

+95
-54
lines changed

5 files changed

+95
-54
lines changed

haskell_edsl/checktestdata.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ library
2828
containers >=0.5 && <0.6,
2929
either >=4.3 && <4.5,
3030
mtl >=2.2 && <2.3,
31+
optparse-applicative >= 0.13.1,
3132
regex-tdfa >= 1.2,
3233
uu-parsinglib >= 2.9
3334
hs-source-dirs: src
@@ -38,6 +39,7 @@ executable checktestdata
3839
main-is: Main.hs
3940
hs-source-dirs: src-main
4041
build-depends: base >=4.7 && <4.10,
42+
optparse-applicative >= 0.13.1,
4143
checktestdata -any
4244
default-language: Haskell2010
4345
ghc-options: -Wall -O2

haskell_edsl/src-main/Main.hs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
module Main where
2-
3-
import Checktestdata
4-
import Checktestdata.Script
5-
6-
import System.Environment ( getArgs, withArgs, getProgName )
7-
import System.Exit ( exitFailure)
8-
9-
main :: IO ()
10-
main = do
11-
args <- getArgs
12-
case args of
13-
[] -> printUsage
14-
(fp:rargs) -> do
15-
ast <- parseScript fp
16-
withArgs rargs $ ctdMain $ interpret ast
17-
18-
printUsage :: IO ()
19-
printUsage = do
20-
nm <- getProgName
21-
putStrLn $ "Usage:"
22-
putStrLn $ " " ++ nm ++ " script.ctd data.in"
23-
putStrLn $ " " ++ nm ++ " script.ctd < data.in"
24-
exitFailure
1+
module Main where
2+
3+
import Checktestdata
4+
import Checktestdata.Script
5+
import Checktestdata.Options
6+
7+
import Options.Applicative
8+
import Data.Semigroup ((<>))
9+
10+
11+
main :: IO ()
12+
main = do
13+
opts <- execParser $ info (mainOpts <**> helper)
14+
( fullDesc
15+
<> progDesc "Check the data for testdata.in with script.ctd"
16+
<> header "checktestdata" )
17+
ast <- parseScript $ script_file opts
18+
ctdMainOpts (script_options opts) $ interpret ast
19+
20+
21+
-- | Parser for the commandline options of a checktestdata script.
22+
mainOpts :: Parser MainOptions
23+
mainOpts = MainOptions
24+
<$> argument str (metavar "script.ctd")
25+
<*> generalOpts
26+
27+
28+
data MainOptions = MainOptions {
29+
script_file :: FilePath,
30+
script_options :: Options
31+
}

haskell_edsl/src-main/Test.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ checkRun :: Options -> FilePath -> FilePath -> IO (Either String ())
8585
checkRun opts prog dataf = do
8686
res <- try $ do
8787
ctd <- parseScript $ testsdir ++ prog
88-
runCTDFile opts (interpret ctd) $ testsdir ++ dataf
88+
let sopts = opts {
89+
input_file = Just $ testsdir ++ dataf,
90+
quiet = True
91+
}
92+
ctdMainOpts sopts (interpret ctd)
8993
case res of
9094
Left e -> return $ Left $ show (e :: SomeException)
91-
Right (Left e) -> return $ Left e
92-
Right (Right _) -> return $ Right ()
95+
Right () -> return $ Right ()

haskell_edsl/src/Checktestdata.hs

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3134
import Checktestdata.Derived
3235
import Checktestdata.Options
3336

34-
import System.Environment ( getArgs, getProgName )
35-
import System.Exit ( exitFailure, exitSuccess)
37+
import System.Exit ( exitFailure )
3638
import 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.
4849
ctdMain :: CTD a -> IO ()
4950
ctdMain 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+

haskell_edsl/src/Checktestdata/Options.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ data Options = Options {
2121
-- | When set to 'True', whitespace changes are accepted, including heading
2222
-- and training whitespace, but not newlines. Be careful: extra whitespace
2323
-- matches greedily!
24-
whitespace_ok :: Bool
24+
whitespace_ok :: Bool,
25+
-- | When set to 'True', print no output.
26+
quiet :: Bool,
27+
-- | Path to the input, or 'Nothing' for reading from stdin
28+
input_file :: Maybe FilePath
2529
}
2630

2731
-- | The default values for 'Options'
2832
defaultOptions :: Options
2933
defaultOptions = Options {
30-
whitespace_ok = False
34+
whitespace_ok = False,
35+
quiet = False,
36+
input_file = Nothing
3137
}

0 commit comments

Comments
 (0)