module Options import Quox.Pretty import System import System.Console.GetOpt import System.File import System.Term import Derive.Prelude %default total %language ElabReflection public export data OutFile = File String | Console | NoOut %name OutFile f %runElab derive "OutFile" [Eq, Show] public export data Phase = Parse | Check | Erase | Scheme | End %name Phase p %runElab derive "Phase" [Eq, Show] ||| a list of all intermediate `Phase`s (excluding `End`) public export %inline allPhases : List Phase allPhases = %runElab do cs <- getCons $ fst !(lookupName "Phase") traverse (check . var) $ fromMaybe [] $ init' cs ||| `Guess` is `Term` for a terminal and `NoHL` for a file public export data HLType = Guess | NoHL | Term | Html %runElab derive "HLType" [Eq, Show] public export record Dump where constructor MkDump parse, check, erase, scheme : OutFile %name Dump dump %runElab derive "Dump" [Show] public export record Options where constructor MkOpts hlType : HLType dump : Dump outFile : OutFile until : Maybe Phase flavor : Pretty.Flavor width : Nat include : List String %name Options opts %runElab derive "Options" [Show] export defaultWidth : IO Nat defaultWidth = do w <- cast {to = Nat} <$> getTermCols pure $ if w == 0 then 80 else w export defaultOpts : IO Options defaultOpts = pure $ MkOpts { hlType = Guess, dump = MkDump NoOut NoOut NoOut NoOut, outFile = Console, until = Nothing, flavor = Unicode, width = !defaultWidth, include = ["."] } private data HelpType = Common | All private data OptAction = ShowHelp HelpType | Err String | Ok (Options -> Options) %name OptAction act private toOutFile : String -> OutFile toOutFile "" = NoOut toOutFile "-" = Console toOutFile f = File f private toPhase : String -> OptAction toPhase str = let lstr = toLower str in case find (\p => toLower (show p) == lstr) allPhases of Just p => Ok $ setPhase p Nothing => Err "unknown phase name \{show str}\nphases: \{phaseNames}" where phaseNames = joinBy ", " $ map (toLower . show) allPhases defConsole : OutFile -> OutFile defConsole NoOut = Console defConsole f = f setPhase : Phase -> Options -> Options setPhase Parse = {until := Just Parse, dump.parse $= defConsole} setPhase Check = {until := Just Check, dump.check $= defConsole} setPhase Erase = {until := Just Erase, dump.erase $= defConsole} setPhase Scheme = {until := Just Scheme, dump.scheme $= defConsole} setPhase End = id private toWidth : String -> OptAction toWidth s = case parsePositive s of Just n => Ok {width := n} Nothing => Err "invalid width: \{show s}" private toHLType : String -> OptAction toHLType str = case toLower str of "none" => Ok {hlType := NoHL} "term" => Ok {hlType := Term} "html" => Ok {hlType := Html} _ => Err "unknown highlighting type \{show str}\ntypes: term, html, none" ||| like ghc, -i '' clears the search path; -i a:b:c adds a,b,c to the end private dirListFlag : String -> List String -> List String dirListFlag arg val = if null arg then [] else val ++ toList (split (== ':') arg) private commonOptDescrs' : List (OptDescr OptAction) commonOptDescrs' = [ MkOpt ['i'] ["include"] (ReqArg (\is => Ok {include $= dirListFlag is}) "