packages feed

visual-graphrewrite-0.4.0.1: GraphRewrite/Main/CmdLineOpts.hs

module GraphRewrite.Main.CmdLineOpts
where

import System.Console.GetOpt
import System.Environment

data Options = Options
    { showVersion   :: Bool             -- ^ just show the version and exit
    , inputFile     :: Maybe String     -- ^ Nothing: stdin
    , outputFile    :: Maybe String     -- ^ Nothing: stdout
    , isVerbose     :: Bool
    , debug         :: Bool
    , mainTerm      :: Maybe String     -- ^ Name of the main term on which to perform rewriting (example: result)
    }

defaultOptions :: Options
defaultOptions = Options
    { showVersion   = False
    , inputFile     = Nothing
    , outputFile    = Nothing
    , isVerbose     = False
    , debug         = False
    , mainTerm      = Nothing
    }

----------------------------------- possible options

options :: [OptDescr (Options -> Options)]
options =
    [ Option ['v']     ["verbose"] (NoArg  (\r -> r {isVerbose   = True}))       "verbose output on stderr"
    , Option ['d']     ["debug"]   (NoArg  (\r -> r {debug       = True}))       "show debug info on stdout"
    , Option ['V','?'] ["version"] (NoArg  (\r -> r {showVersion = True}))       "show version number"
    , Option ['o']     ["output"]  (ReqArg (\f r -> r {outputFile = Just f}) "FILE")  "output FILE"
    , Option ['i']     ["input"]   (ReqArg (\f r -> r {inputFile  = Just f}) "FILE")  "input FILE"
    , Option ['m']     ["main"]    (ReqArg (\m r -> r {mainTerm = Just m}) "TERM") "main term on which to perform rewriting"
    ]

----------------------------------- parse options

addInput :: String -> Options -> Options
addInput f r = r {inputFile = Just f}

parseOptions :: [String] -> IO Options
parseOptions argv = do
    progName <- getProgName
    let header = "Usage: " ++ progName ++ " [OPTION...] files..."
    case getOpt Permute options argv of
      (o,n,[]  ) -> return $ let
                        o'  = foldl (flip id) defaultOptions o
                    in foldl (flip addInput) o' n
      (_,_,errs) -> error (concat errs ++ usageInfo header options)