on master help sign in
[ fix #4255 ] Not silently accepting invalid options
377d87aGuillaume Allais
src/Idris/CmdOptions.hs+7−3
@@ -10,7 +10,8 @@ Maintainer : The Idris Community.
10
10
11module Idris.CmdOptions
11module Idris.CmdOptions
12 ( opt, getClient, getPkg, getPkgCheck, getPkgClean, getPkgMkDoc,
12 ( opt, getClient, getPkg, getPkgCheck, getPkgClean, getPkgMkDoc,
13− getPkgREPL, getPkgTest, getPort, getIBCSubDir, pureArgParser, runArgParser
13+ getPkgREPL, getPkgTest, getPort, getIBCSubDir,
14+ pureArgParser, execArgParserPure, runArgParser
14 ) where
15 ) where
15
16
16import Idris.Info (getIdrisVersion)
17import Idris.Info (getIdrisVersion)
@@ -72,10 +73,13 @@ runArgParser = do opts <- execParser $ info parser
72 PP.empty,
73 PP.empty,
73 PP.indent 4 (PP.text "http://www.idris-lang.org/")]
74 PP.indent 4 (PP.text "http://www.idris-lang.org/")]
74
75
76+execArgParserPure :: [String] -> ParserResult [Opt]
77+execArgParserPure args = preProcOpts <$> execParserPure (prefs idm) (info parser idm) args
78+
75pureArgParser :: [String] -> [Opt]
79pureArgParser :: [String] -> [Opt]
76−pureArgParser args = case getParseResult $ execParserPure (prefs idm) (info parser idm) args of
80+pureArgParser args = case getParseResult (execArgParserPure args) of
77 Just opts -> preProcOpts opts
81 Just opts -> preProcOpts opts
78− Nothing -> []
82+ Nothing -> []
79
83
80parser :: Parser [Opt]
84parser :: Parser [Opt]
81parser = runA $ proc () -> do
85parser = runA $ proc () -> do
src/Idris/Package/Parser.hs+11−1
@@ -10,6 +10,7 @@ module Idris.Package.Parser where
10
10
11import Idris.CmdOptions
11import Idris.CmdOptions
12import Idris.Imports
12import Idris.Imports
13+import Idris.Options (Opt)
13import Idris.Package.Common
14import Idris.Package.Common
14import Idris.Parser (moduleName)
15import Idris.Parser (moduleName)
15import Idris.Parser.Helpers (Parser, Parsing, eol, iName, identifier, isEol,
16import Idris.Parser.Helpers (Parser, Parsing, eol, iName, identifier, isEol,
@@ -19,6 +20,7 @@ import Idris.Parser.Helpers (Parser, Parsing, eol, iName, identifier, isEol,
19import Control.Applicative
20import Control.Applicative
20import Control.Monad.State.Strict
21import Control.Monad.State.Strict
21import Data.List (union)
22import Data.List (union)
23+import qualified Options.Applicative as Opts
22import System.Directory (doesFileExist)
24import System.Directory (doesFileExist)
23import System.Exit
25import System.Exit
24import System.FilePath (isValid, takeExtension, takeFileName)
26import System.FilePath (isValid, takeExtension, takeFileName)
@@ -109,11 +111,19 @@ clause name p f = do value <- reserved name *> lchar '=' *> p <* someSpace
109commaSep :: Parsing m => m a -> m [a]
111commaSep :: Parsing m => m a -> m [a]
110commaSep p = P.sepBy1 p (lchar ',')
112commaSep p = P.sepBy1 p (lchar ',')
111
113
114+pOptions :: PParser [Opt]
115+pOptions = do
116+ str <- stringLiteral
117+ case execArgParserPure (words str) of
118+ Opts.Success a -> return a
119+ Opts.Failure e -> fail $ fst $ Opts.renderFailure e ""
120+ _ -> fail "Unexpected error"
121+
112pClause :: PParser ()
122pClause :: PParser ()
113pClause = clause "executable" filename (\st v -> st { execout = Just v })
123pClause = clause "executable" filename (\st v -> st { execout = Just v })
114 <|> clause "main" (iName []) (\st v -> st { idris_main = Just v })
124 <|> clause "main" (iName []) (\st v -> st { idris_main = Just v })
115 <|> clause "sourcedir" identifier (\st v -> st { sourcedir = v })
125 <|> clause "sourcedir" identifier (\st v -> st { sourcedir = v })
116− <|> clause "opts" (pureArgParser . words <$> stringLiteral) (\st v -> st { idris_opts = v ++ idris_opts st })
126+ <|> clause "opts" pOptions (\st v -> st { idris_opts = v ++ idris_opts st })
117 <|> clause "pkgs" (commaSep (pPkgName <* someSpace)) (\st ps ->
127 <|> clause "pkgs" (commaSep (pPkgName <* someSpace)) (\st ps ->
118 let pkgs = pureArgParser $ concatMap (\x -> ["-p", show x]) ps
128 let pkgs = pureArgParser $ concatMap (\x -> ["-p", show x]) ps
119 in st { pkgdeps = ps `union` pkgdeps st
129 in st { pkgdeps = ps `union` pkgdeps st
test/TestData.hs+2−1
@@ -205,7 +205,8 @@ testFamiliesData = [
205 ( 5, ANY ),
205 ( 5, ANY ),
206 ( 6, ANY ),
206 ( 6, ANY ),
207 ( 7, ANY ),
207 ( 7, ANY ),
208− ( 8, ANY )]),
208+ ( 8, ANY ),
209+ ( 10, ANY )]),
209 ("prelude", "Prelude",
210 ("prelude", "Prelude",
210 [ ( 1, ANY )]),
211 [ ( 1, ANY )]),
211 ("primitives", "Primitive types",
212 ("primitives", "Primitive types",
test/pkg010/expectednew file+28
@@ -0,0 +1,28 @@
1+Uncaught error: user error (wrongopts.ipkg:4:1:
2+ |
3+4 | <empty line>
4+ | ^
5+Invalid option `-total'
6+
7+Usage: ([--nobanner] | [-q|--quiet] | [--ide-mode] | [--ide-mode-socket] |
8+ [--client ARG] | [--log LEVEL] | [--logging-categories CATS] |
9+ [--nobasepkgs] | [--noprelude] | [--nobuiltins] | [--check] |
10+ [-o|--output FILE] | [--interface] | [--typeintype] | [--total] |
11+ [--partial] | [--warnpartial] | [--warnreach] | [--warnipkg] |
12+ [--nocoverage] | [--errorcontext] | [--info] | [--listlogcats] |
13+ [--link] | [--listlibs] | [--libdir] | [--docdir] | [--include] | [--V2]
14+ | [--V1] | [-V|--V0|--verbose] | [--ibcsubdir FILE] |
15+ [-i|--idrispath ARG] | [--sourcepath ARG] | [--warn] |
16+ [-p|--package ARG] | [--port PORT] | [--build IPKG] | [--install IPKG] |
17+ [--repl IPKG] | [--clean IPKG] | [--mkdoc IPKG] | [--installdoc IPKG] |
18+ [--checkpkg IPKG] | [--testpkg IPKG] | [--indent-with INDENT] |
19+ [--indent-clause INDENT] | [--bytecode ARG] | [-S|--codegenonly] |
20+ [-c|--compileonly] | [--dumpdefuns ARG] | [--dumpcases ARG] |
21+ [--codegen TARGET] | [--portable-codegen TARGET] | [--cg-opt ARG] |
22+ [-e|--eval EXPR] | [--execute] | [--exec EXPR] | [-X|--extension EXT] |
23+ [--O3] | [--O2] | [--O1] | [--O0] | [--partial-eval] |
24+ [--no-partial-eval] | [-O|--level ARG] | [--target TRIPLE] | [--cpu CPU]
25+ | [--color|--colour] | [--nocolor|--nocolour] | [--consolewidth WIDTH] |
26+ [--highlight] | [--no-elim-deprecation-warnings] |
27+ [--no-tactic-deprecation-warnings]) [FILES] [-v|--version]
28+)
test/pkg010/runnew file+2
@@ -0,0 +1,2 @@
1+#!/usr/bin/env bash
2+${IDRIS:-idris} $@ --build wrongopts.ipkg
test/pkg010/wrongopts.ipkgnew file+3
@@ -0,0 +1,3 @@
1+package wrongopts
2+
3+opts = "-total"