Skip to content

Commit a983142

Browse files
committed
report more option errors
1 parent 6732c5e commit a983142

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/QuickBench.hs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ Options:
5454
-- quickbench.cabal
5555
-- README.md
5656
-- quickbench.1.md
57-
-- any Just assumptions below, if changing [default] annotations.
57+
-- unknown option checking below
58+
-- Just assumptions below, if changing [default] annotations.
5859
-- Try to avoid writing the same thing different ways in all of these places.
5960

6061
defaultFile :: FilePath
6162
defaultFile = "bench.sh"
6263

6364
data Opts = Opts {
64-
-- docopts :: Arguments,
65+
docopts :: Arguments,
6566
file :: Maybe FilePath
6667
,executables :: [String]
6768
,iterations :: Int
@@ -90,7 +91,7 @@ getOpts = do
9091
precision' <- readint $ fromJust $ option "precision"
9192
let
9293
opts = Opts {
93-
-- docopts = dopts,
94+
docopts = dopts,
9495
file = option "file"
9596
,executables = maybe [] (splitOn ",") $ option "with"
9697
,iterations = iterations'
@@ -104,10 +105,24 @@ getOpts = do
104105
}
105106
when (debug opts || "--debug" `elem` lateflags) $ err $ ppShow opts ++ "\n"
106107
when (help opts) $ putStrLn (usage docoptpatterns) >> exitSuccess
107-
unless (null lateflags) $
108-
fail $ printf "option %s should appear before argument %s"
109-
(show $ head lateflags)
110-
(show $ head args) -- safe, we don't see lateflags without some regular args
108+
-- try to report some errors docopts misses
109+
case (lateflags, args) of
110+
-- unknown option
111+
-- quickbench a -fk user error (unknown option: "fk")
112+
-- (f:_,[]) | not $ elem f [
113+
-- "file","f"
114+
-- ,"with","w"
115+
-- ,"iterations","n"
116+
-- ,"cycles","N"
117+
-- ,"precision","p"
118+
-- ,"verbose","v"
119+
-- ,"more-verbose","V"
120+
-- ] -> fail $ printf "unknown option: %s" (show f)
121+
-- option value missing
122+
(f:_,[]) -> fail $ printf "option %s needs a value or is unknown" (show f)
123+
-- option following arguments
124+
(f:_,a:_) -> fail $ printf "option %s should appear before argument %s or is unknown" (show f) (show a)
125+
_ -> return ()
111126
return opts
112127

113128
-- | Run the quickbench program, returning an error message if there was a problem.

0 commit comments

Comments
 (0)