packages feed

ftshell-0.2: src/main.hs



-- The main module of the application.
-- Processes the command line and runs the shell.
module Main where



import Paths_ftshell (getDataFileName, version)

import Data.Maybe (isNothing)
import Data.Version (showVersion)
import System.Console.GetOpt
import System.Environment (getProgName, getArgs)

import Settings 
import Shell (initAndRunShell)



-- The main entry point of the application.
-- Processes the command line options.
-- Shows help or version information, or starts the shell.
main :: IO ()
main = do
  settings <- initialSettings >>= parseOptions
  case insteadOfShell settings of
    Just PrintHelp    -> printHelp
    Just PrintVersion -> printVersion settings
    Nothing           -> initAndRunShell settings





------- Getting the options ----------------------------------------------------



-- Possible command line option flags.
data Flag
  = ShowHelp            -- show the help message
  | ShowVersion         -- show the version message
  | LoadFile String     -- load an additional declarations file
  | DontLoadPrelude     -- don't load the prelude file
  deriving Eq



-- The options menu.
options :: [OptDescr Flag]
options =
  [ Option ['h'] ["help"] (NoArg ShowHelp)
      "show this help message and exit"
  , Option ['l'] ["load"] (ReqArg LoadFile "FILE")
      "load an additional file of declarations"
  , Option ['n'] ["no-prelude"] (NoArg DontLoadPrelude)
      "don't load the basic file of declarations"
  , Option ['v'] ["version"] (NoArg ShowVersion)
      "show version information and exit"
  ]



-- Maps the options to the settings.
parseOptions :: Settings -> IO Settings
parseOptions settings = do
  args <- getArgs
  case getOpt Permute options args of
    (os, _, []) -> return $ foldl apply settings os
    (_,  _, es) -> do putStr (unlines es)
                      return $ settings { insteadOfShell = Just PrintHelp }



-- Applies one command line option to some given settings.
apply :: Settings -> Flag -> Settings

apply sets ShowHelp = sets { insteadOfShell = Just PrintHelp }

apply sets ShowVersion = if isNothing (insteadOfShell sets)
                           then sets { insteadOfShell = Just PrintVersion }
                           else sets

apply sets (LoadFile s) = sets { inputFiles = inputFiles sets ++ [s] } 

apply sets DontLoadPrelude = sets { prelude = Nothing } 






------- Printing messages ------------------------------------------------------



-- Shows the available command line options.
printHelp :: IO ()
printHelp = do
  progName <- getProgName
  putStr $ usageInfo ("Usage: " ++ progName ++ " [OPTION]...") options
  putStr $ unlines
    [ ""
    , "The application loads first the basic declarations file before every"
    , "file specified by the `--load' option is loaded in the order given at"
    , "the command line."
    , ""
    ]



-- Shows the version and additional information.
printVersion :: Settings -> IO ()
printVersion sets = do
  declFile <- getDataFileName basicDeclarationsFileName
  putStr $ unlines 
    [ "FTshell, version " ++ showVersion version
    , "Automatic generation of free theorems"
    , ""
    , "Basic declarations file: " ++ declFile
    , "Parser: " ++ parserName sets
    , "Shell backend: " ++ backendName sets
    , ""
    , "Usage: For basic information, try the `--help' option."
    ]