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