on 008fb1f help sign in
Idris-dev/src/Idris/CmdOptions.hs
296 lines 15 kB history raw
1{-# LANGUAGE CPP #-}
2{-|
3Module : Idris.CmdOptions
4Description : A parser for the CmdOptions for the Idris executable.
5License : BSD3
6Maintainer : The Idris Community.
7-}
9{-# LANGUAGE Arrows #-}
11module Idris.CmdOptions
12 (
13 module Idris.CmdOptions
14 , opt
15 , getClient, getPkg, getPkgCheck, getPkgClean, getPkgMkDoc
16 , getPkgREPL, getPkgTest, getPort, getIBCSubDir
17 ) where
19import Idris.AbsSyntax (getClient, getIBCSubDir, getPkg, getPkgCheck,
20 getPkgClean, getPkgMkDoc, getPkgREPL, getPkgTest,
21 getPort, opt)
22import Idris.AbsSyntaxTree
23import Idris.Info (getIdrisVersion)
24import IRTS.CodegenCommon
26import Control.Monad.Trans (lift)
27import Control.Monad.Trans.Except (throwE)
28import Control.Monad.Trans.Reader (ask)
29import Data.Char
30import Data.Maybe
31#if MIN_VERSION_optparse_applicative(0,13,0)
32import Data.Monoid ((<>))
33#endif
34import Options.Applicative
35import Options.Applicative.Arrows
36import Options.Applicative.Types (ReadM(..))
38import Text.ParserCombinators.ReadP hiding (many, option)
40import Safe (lastMay)
41import qualified Text.PrettyPrint.ANSI.Leijen as PP
43runArgParser :: IO [Opt]
44runArgParser = do opts <- execParser $ info parser
45 (fullDesc
46 <> headerDoc (Just idrisHeader)
47 <> progDescDoc (Just idrisProgDesc)
48 <> footerDoc (Just idrisFooter)
49 )
50 return $ preProcOpts opts
51 where
52 idrisHeader = PP.hsep [PP.text "Idris version", PP.text getIdrisVersion, PP.text ", (C) The Idris Community 2016"]
53 idrisProgDesc = PP.vsep [PP.empty,
54 PP.text "Idris is a general purpose pure functional programming language with dependent",
55 PP.text "types. Dependent types allow types to be predicated on values, meaning that",
56 PP.text "some aspects of a program's behaviour can be specified precisely in the type.",
57 PP.text "It is compiled, with eager evaluation. Its features are influenced by Haskell",
58 PP.text "and ML.",
59 PP.empty,
60 PP.vsep $ map (PP.indent 4 . PP.text) [
61 "+ Full dependent types with dependent pattern matching",
62 "+ Simple case expressions, where-clauses, with-rule",
63 "+ Pattern matching let- and lambda-bindings",
64 "+ Overloading via Interfaces (Type class-like), Monad comprehensions",
65 "+ do-notation, idiom brackets",
66 "+ Syntactic conveniences for lists, tuples, dependent pairs",
67 "+ Totality checking",
68 "+ Coinductive types",
69 "+ Indentation significant syntax, Extensible syntax",
70 "+ Tactic based theorem proving (influenced by Coq)",
71 "+ Cumulative universes",
72 "+ Simple Foreign Function Interface",
73 "+ Hugs style interactive environment"
74 ]]
75 idrisFooter = PP.vsep [PP.text "It is important to note that Idris is first and foremost a research tool",
76 PP.text "and project. Thus the tooling provided and resulting programs created",
77 PP.text "should not necessarily be seen as production ready nor for industrial use.",
78 PP.empty,
79 PP.text "More details over Idris can be found online here:",
80 PP.empty,
81 PP.indent 4 (PP.text "http://www.idris-lang.org/")]
83pureArgParser :: [String] -> [Opt]
84pureArgParser args = case getParseResult $ execParserPure (prefs idm) (info parser idm) args of
85 Just opts -> preProcOpts opts
86 Nothing -> []
88parser :: Parser [Opt]
89parser = runA $ proc () -> do
90 flags <- asA parseFlags -< ()
91 files <- asA (many $ argument (fmap Filename str) (metavar "FILES")) -< ()
92 A parseVersion >>> A helper -< (flags ++ files)
94parseFlags :: Parser [Opt]
95parseFlags = many $
96 flag' NoBanner (long "nobanner" <> help "Suppress the banner")
97 <|> flag' Quiet (short 'q' <> long "quiet" <> help "Quiet verbosity")
99 -- IDE Mode Specific Flags
100 <|> flag' Idemode (long "ide-mode" <> help "Run the Idris REPL with machine-readable syntax")
101 <|> flag' IdemodeSocket (long "ide-mode-socket" <> help "Choose a socket for IDE mode to listen on")
103 -- Client flags
104 <|> Client <$> strOption (long "client")
106 -- Logging Flags
107 <|> OLogging <$> option auto (long "log" <> metavar "LEVEL" <> help "Debugging log level")
108 <|> OLogCats <$> option (str >>= parseLogCats)
109 (long "logging-categories"
110 <> metavar "CATS"
111 <> help "Colon separated logging categories. Use --listlogcats to see list.")
113 -- Turn off things
114 <|> flag' NoBasePkgs (long "nobasepkgs" <> help "Do not use the given base package")
115 <|> flag' NoPrelude (long "noprelude" <> help "Do not use the given prelude")
116 <|> flag' NoBuiltins (long "nobuiltins" <> help "Do not use the builtin functions")
118 <|> flag' NoREPL (long "check" <> help "Typecheck only, don't start the REPL")
120 <|> Output <$> strOption (short 'o' <> long "output" <> metavar "FILE" <> help "Specify output file")
122 -- <|> flag' TypeCase (long "typecase")
123 <|> flag' Interface (long "interface" <> help "Generate interface files from ExportLists")
124 <|> flag' TypeInType (long "typeintype" <> help "Turn off Universe checking")
125 <|> flag' DefaultTotal (long "total" <> help "Require functions to be total by default")
126 <|> flag' DefaultPartial (long "partial")
127 <|> flag' WarnPartial (long "warnpartial" <> help "Warn about undeclared partial functions")
128 <|> flag' WarnReach (long "warnreach" <> help "Warn about reachable but inaccessible arguments")
129 <|> flag' NoCoverage (long "nocoverage")
130 <|> flag' ErrContext (long "errorcontext")
132 -- Show things
133 <|> flag' ShowAll (long "info" <> help "Display information about installation.")
134 <|> flag' ShowLoggingCats (long "listlogcats" <> help "Display logging categories")
135 <|> flag' ShowLibs (long "link" <> help "Display link flags")
136 <|> flag' ShowPkgs (long "listlibs" <> help "Display installed libraries")
137 <|> flag' ShowLibDir (long "libdir" <> help "Display library directory")
138 <|> flag' ShowDocDir (long "docdir" <> help "Display idrisdoc install directory")
139 <|> flag' ShowIncs (long "include" <> help "Display the includes flags")
141 <|> flag' (Verbose 3) (long "V2" <> help "Loudest verbosity")
142 <|> flag' (Verbose 2) (long "V1" <> help "Louder verbosity")
143 <|> flag' (Verbose 1) (short 'V' <> long "V0" <>long "verbose" <> help "Loud verbosity")
145 <|> IBCSubDir <$> strOption (long "ibcsubdir" <> metavar "FILE" <> help "Write IBC files into sub directory")
146 <|> ImportDir <$> strOption (short 'i' <> long "idrispath" <> help "Add directory to the list of import paths")
147 <|> SourceDir <$> strOption (long "sourcepath" <> help "Add directory to the list of source search paths")
149 <|> flag' WarnOnly (long "warn")
151 <|> Pkg <$> strOption (short 'p' <> long "package" <> help "Add package as a dependency")
152 <|> Port <$> option portReader (long "port" <> metavar "PORT" <> help "REPL TCP port - pass \"none\" to not bind any port")
154 -- Package commands
155 <|> PkgBuild <$> strOption (long "build" <> metavar "IPKG" <> help "Build package")
156 <|> PkgInstall <$> strOption (long "install" <> metavar "IPKG" <> help "Install package")
157 <|> PkgREPL <$> strOption (long "repl" <> metavar "IPKG" <> help "Launch REPL, only for executables")
158 <|> PkgClean <$> strOption (long "clean" <> metavar "IPKG" <> help "Clean package")
159 <|> (PkgDocBuild <$> strOption (long "mkdoc" <> metavar "IPKG" <> help "Generate IdrisDoc for package"))
160 <|> PkgDocInstall <$> strOption (long "installdoc" <> metavar "IPKG" <> help "Install IdrisDoc for package")
161 <|> PkgCheck <$> strOption (long "checkpkg" <> metavar "IPKG" <> help "Check package only")
162 <|> PkgTest <$> strOption (long "testpkg" <> metavar "IPKG" <> help "Run tests for package")
164 -- Interactive Editing Flags
165 <|> IndentWith <$> option auto (long "indent-with"
166 <> metavar "INDENT"
167 <> help "Indentation to use with :makewith (default 2)")
168 <|> IndentClause <$> option auto (long "indent-clause"
169 <> metavar "INDENT"
170 <> help "Indentation to use with :addclause (default 2)")
172 -- Misc options
173 <|> BCAsm <$> strOption (long "bytecode")
175 <|> flag' (OutputTy Raw) (short 'S' <> long "codegenonly" <> help "Do no further compilation of code generator output")
176 <|> flag' (OutputTy Object) (short 'c' <> long "compileonly" <> help "Compile to object files rather than an executable")
178 <|> DumpDefun <$> strOption (long "dumpdefuns")
179 <|> DumpCases <$> strOption (long "dumpcases")
181 <|> (UseCodegen . parseCodegen) <$> strOption (long "codegen"
182 <> metavar "TARGET"
183 <> help "Select code generator: C, Javascript, Node and bytecode are bundled with Idris")
185 <|> ((UseCodegen . Via JSONFormat) <$> strOption (long "portable-codegen"
186 <> metavar "TARGET"
187 <> help "Pass the name of the code generator. This option is for codegens that take JSON formatted IR."))
189 <|> CodegenArgs <$> strOption (long "cg-opt"
190 <> metavar "ARG"
191 <> help "Arguments to pass to code generator")
193 <|> EvalExpr <$> strOption (long "eval"
194 <> short 'e'
195 <> metavar "EXPR"
196 <> help "Evaluate an expression without loading the REPL")
198 <|> flag' (InterpretScript "Main.main") (long "execute" <> help "Execute as idris")
199 <|> InterpretScript <$> strOption (long "exec" <> metavar "EXPR" <> help "Execute as idris")
201 <|> ((Extension . getExt) <$> strOption (long "extension"
202 <> short 'X'
203 <> metavar "EXT"
204 <> help "Turn on language extension (TypeProviders or ErrorReflection)"))
206 -- Optimisation Levels
207 <|> flag' (OptLevel 3) (long "O3")
208 <|> flag' (OptLevel 2) (long "O2")
209 <|> flag' (OptLevel 1) (long "O1")
210 <|> flag' (OptLevel 0) (long "O0")
212 <|> flag' (AddOpt PETransform) (long "partial-eval")
213 <|> flag' (RemoveOpt PETransform) (long "no-partial-eval" <> help "Switch off partial evaluation, mainly for debugging purposes")
215 <|> OptLevel <$> option auto (short 'O' <> long "level")
217 <|> TargetTriple <$> strOption (long "target" <> metavar "TRIPLE" <> help "If supported the codegen will target the named triple.")
218 <|> TargetCPU <$> strOption (long "cpu" <> metavar "CPU" <> help "If supported the codegen will target the named CPU e.g. corei7 or cortex-m3")
220 -- Colour Options
221 <|> flag' (ColourREPL True) (long "colour" <> long "color" <> help "Force coloured output")
222 <|> flag' (ColourREPL False) (long "nocolour" <> long "nocolor" <> help "Disable coloured output")
224 <|> (UseConsoleWidth <$> option (str >>= parseConsoleWidth) (long "consolewidth" <> metavar "WIDTH" <> help "Select console width: auto, infinite, nat"))
226 <|> flag' DumpHighlights (long "highlight" <> help "Emit source code highlighting")
228 <|> flag' NoElimDeprecationWarnings (long "no-elim-deprecation-warnings" <> help "Disable deprecation warnings for %elim")
229 <|> flag' NoOldTacticDeprecationWarnings (long "no-tactic-deprecation-warnings" <> help "Disable deprecation warnings for the old tactic sublanguage")
231 where
232 getExt :: String -> LanguageExt
233 getExt s = fromMaybe (error ("Unknown extension " ++ s)) (maybeRead s)
234 maybeRead :: String -> Maybe LanguageExt
235 maybeRead = fmap fst . listToMaybe . reads
236 portReader :: ReadM REPLPort
237 portReader =
238 ((ListenPort . fromIntegral) <$> auto) <|>
239 (ReadM $ do opt <- ask
240 if map toLower opt == "none"
241 then return $ DontListen
242 else lift $ throwE $ ErrorMsg $
243 "got " <> opt <> " expected port number or \"none\"")
245parseVersion :: Parser (a -> a)
246parseVersion = infoOption getIdrisVersion (short 'v' <> long "version" <> help "Print version information")
248preProcOpts :: [Opt] -> [Opt]
249preProcOpts (NoBuiltins : xs) = NoBuiltins : NoPrelude : preProcOpts xs
250preProcOpts (Output s : xs) = Output s : NoREPL : preProcOpts xs
251preProcOpts (BCAsm s : xs) = BCAsm s : NoREPL : preProcOpts xs
252preProcOpts (x:xs) = x : preProcOpts xs
253preProcOpts [] = []
255parseCodegen :: String -> Codegen
256parseCodegen "bytecode" = Bytecode
257parseCodegen cg = Via IBCFormat (map toLower cg)
259parseLogCats :: Monad m => String -> m [LogCat]
260parseLogCats s =
261 case lastMay (readP_to_S doParse s) of
262 Just (xs, _) -> return xs
263 _ -> fail "Incorrect categories specified"
264 where
265 doParse :: ReadP [LogCat]
266 doParse = do
267 cs <- sepBy1 parseLogCat (char ':')
268 eof
269 return (concat cs)
271 parseLogCat :: ReadP [LogCat]
272 parseLogCat = (string (strLogCat IParse) *> return parserCats)
273 <|> (string (strLogCat IElab) *> return elabCats)
274 <|> (string (strLogCat ICodeGen) *> return codegenCats)
275 <|> (string (strLogCat ICoverage) *> return [ICoverage])
276 <|> (string (strLogCat IIBC) *> return [IIBC])
277 <|> (string (strLogCat IErasure) *> return [IErasure])
278 <|> parseLogCatBad
280 parseLogCatBad :: ReadP [LogCat]
281 parseLogCatBad = do
282 s <- look
283 fail $ "Category: " ++ s ++ " is not recognised."
285parseConsoleWidth :: Monad m => String -> m ConsoleWidth
286parseConsoleWidth "auto" = return AutomaticWidth
287parseConsoleWidth "infinite" = return InfinitelyWide
288parseConsoleWidth s =
289 case lastMay (readP_to_S integerReader s) of
290 Just (r, _) -> return $ ColsWide r
291 _ -> fail $ "Cannot parse: " ++ s
293integerReader :: ReadP Int
294integerReader = do
295 digits <- many1 $ satisfy isDigit
296 return $ read digits