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, Ord, Show] public export data Phase = Parse | Check | Erase | Scheme %name Phase p %runElab derive "Phase" [Eq, Ord, Show] ||| a list of all `Phase`s public export %inline allPhases : List Phase allPhases = %runElab do -- as a script so it stays up to date cs <- getCons $ fst !(lookupName "Phase") traverse (check . var) cs ||| "guess" is Term for a terminal and NoHL for a file public export data HLType = Guess | NoHL | Term | Html %runElab derive "HLType" [Eq, Ord, Show] public export record Options where constructor MkOpts hlType : HLType 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, 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 -> OptAction toOutFile "" = Ok {outFile := NoOut} toOutFile "-" = Ok {outFile := Console} toOutFile f = Ok {outFile := 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 {until := Just p} Nothing => Err "unknown phase name \{show str}\nphases: \{phaseNames}" where phaseNames = joinBy ", " $ map (toLower . show) allPhases 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 \{str}\ntypes: term, html, none" private commonOptDescrs' : List (OptDescr OptAction) commonOptDescrs' = [ MkOpt ['i'] ["include"] (ReqArg (\i => Ok {include $= (i ::)}) "