[added functionalities to run compiled program in batch, interactive , or command-line mode
martin.hofmann@uni-bamberg.de**20090514093347] hunk ./src/Main.hs 4
-import UI.UIStarter
hunk ./src/Main.hs 5
-main = startUI
+import System.Console.GetOpt
+import System.Environment    
+import System.Exit
+import System.Directory (doesFileExist, doesDirectoryExist)
+import Data.Maybe ( fromMaybe, isJust )
+import Control.Monad (when)
+
+import qualified UI.UIStarter as UI
+
+--main = startUI
+main = do
+    a     <-  getArgs
+    (o,c) <- igorOpts a    
+    putStrLn.unwords $ "ARGs: " : a
+    putStrLn $ "OPTs: " ++ (show o)    
+    putStrLn.unwords $ "CMDs: " : c    
+    startUIWith (o,c) 
+
+--getArgs >>= compilerOpts >>= startUIWith
hunk ./src/Main.hs 25
+data Options = Options
+ { optVerbose     :: Bool
+ , optNoBanner    :: Bool
+ , optShowVersion :: Bool
+ , optShowHelp    :: Bool
+ , optBatch       :: Maybe FilePath
+ } deriving Show
+
+defaultOptions    = Options
+ { optVerbose     = False
+ , optNoBanner    = False
+ , optShowVersion = False
+ , optShowHelp    = False
+ , optBatch       = Nothing
+ }
+
+options :: [OptDescr (Options -> Options)]
+options =
+ [ Option ['v']     ["verbose"]
+     (NoArg (\ opts -> opts { optVerbose = True }))
+     "Chatty output on stderr"
+ , Option ['V'] ["version"]
+     (NoArg (\ opts -> opts { optShowVersion = True }))
+     "Show version number"
+ , Option ['h', '?'] ["help"]
+     (NoArg (\ opts -> opts { optShowHelp = True }))
+     "Show Igor commands"
+ , Option [] ["no-banner"]
+     (NoArg (\ opts -> opts { optNoBanner = True }))
+     "Do not output banner on startup"
+ , Option ['b']     ["batch"]
+     (OptArg ((\ f opts -> opts { optBatch = Just f }) . fromMaybe "batch")
+             "FILE")
+     "Run in batch mode with batch FILE"
+ ]
+
+igorOpts :: [String] -> IO (Options, [String])
+igorOpts argv =
+   case getOpt Permute options argv of
+      (o,n,[]  ) -> return (foldl (flip id) defaultOptions o, n)
+      (_,_,errs) -> ioError (userError (concat errs ++ usage))
+
+usage = usageInfo header options
+    where
+    header = "Usage: igor [OPTION...] [CMD1 , CMD2 , ...]"
hunk ./src/Main.hs 71
- 
-   
+startUIWith :: (Options, [String]) -> IO()
+startUIWith (opts,cmd) = do
+    let state = UI.defaultState{ UI.verbose=optVerbose opts
+                               , UI.noBanner=optNoBanner opts}
+    when (optShowHelp opts) $ putStrLn ("IgorII " ++ UI.version) >>   
+                                        putStrLn usage >>
+                                        UI.showHelp >> return ()
+    when (optShowVersion opts) $ putStrLn ("IgorII " ++ UI.version) >> return ()
+    if (isJust.optBatch $ opts)
+      then do let (Just f) = optBatch opts
+              e <- doesFileExist f
+              if e then UI.runBatch state f
+                else print ("No such file: " ++ (show f)) >> 
+                     exitWith (ExitFailure 1)
+      else if (not.null $ cmd) 
+              then UI.runCmdLine state cmd
+              else UI.runInteractive state
hunk ./src/UI/REPLoop.hs 11
-    repl_eval :: s -> String -> IO (Bool, s),           -- quit flag and new state
+    repl_eval :: s -> String -> IO (Bool, s),   -- quit flag and new state
hunk ./src/UI/UIStarter.hs 44
-startUI :: IO ()
-startUI = getArgs >>= startUIWith
-
-startUIWith :: [String] -> IO()
-startUIWith args = 
-    let state = defaultState
-    in case args of
-        []      -> runInteractive state
-        (f:_)   -> do 
-                    e <- doesFileExist f
-                    if e then
-                        runBatch state f
-                      else          
-                        usage >> exitWith (ExitFailure 1)
+--startUI :: IO ()
+--startUI = getArgs >>= startUIWith
+--
+--startUIWith :: [String] -> IO()
+--startUIWith args = 
+--    let state = defaultState
+--    in case args of
+--        []      -> runInteractive state
+--        (f:_)   -> do 
+--                    e <- doesFileExist f
+--                    if e then
+--                        runBatch state f
+--                      else          
+--                        usage >> exitWith (ExitFailure 1)
hunk ./src/UI/UIStarter.hs 60
-    { verbose  :: Bool
+    { noBanner :: Bool
+    , verbose  :: Bool
hunk ./src/UI/UIStarter.hs 79
-    { verbose   = True
+    { noBanner  = False
+    , verbose   = True
hunk ./src/UI/UIStarter.hs 89
+    
hunk ./src/UI/UIStarter.hs 101
-    sayHello
-    putStrLn $ "Type :h to get help."
+    sayHello True
hunk ./src/UI/UIStarter.hs 106
-    sayHello
+    when (not.noBanner $ state) $ sayHello False
hunk ./src/UI/UIStarter.hs 112
-      else readEvalPrintLoop (genRepl state')
+      else readEvalPrintLoop (genRepl state')      
+    
+runCmdLine :: EnvState -> [String] -> IO ()
+runCmdLine state cmd = do
+    when (not.noBanner $ state) $ sayHello False
+    eval state (unwords cmd) >> return ()
hunk ./src/UI/UIStarter.hs 157
-sayHello :: IO ()
-sayHello = do 
+sayHello :: Bool -> IO ()
+sayHello isInteractive = do 
hunk ./src/UI/UIStarter.hs 161
+    when isInteractive $ putStrLn $ "Type :h to get help."
hunk ./src/UI/UIStarter.hs 175
-    displayIO stdout $ render s $ helpText 
+    showHelp 
hunk ./src/UI/UIStarter.hs 180
-helpText = linebreak <> text "Commands (may be abbreviated):" <> 
-           linebreak <> vcat (map getHelp commands) <> linebreak
+helpText = text "Interactive commands (may be abbreviated):" <$> 
+           vcat (map getHelp commands)
hunk ./src/UI/UIStarter.hs 188
+showHelp :: IO () 
+showHelp = putStrLn.displayStatic $ helpText
+
hunk ./src/UI/UIStarter.hs 427
+         , identLetter    = alphaNum <|> oneOf "_'."
+         
hunk ./src/UI/UIStarter.hs 433
---parens        = P.parens lexer
---semi          = P.semi lexer
---colon         = P.colon lexer
-semiSep1      = T.semiSep1 lexer
+--semiSep1      = T.semiSep1 lexer
+commaSep1     = T.commaSep1 lexer
hunk ./src/UI/UIStarter.hs 439
+identifier    = T.identifier lexer
hunk ./src/UI/UIStarter.hs 451
-           ; x <- semiSep1 pCommand
+           ; x <- commaSep1 pCommand
hunk ./src/UI/UIStarter.hs 493
-            ; i <- stringLiteral
+            ; i <- identifier
hunk ./src/UI/UIStarter.hs 499
-pString c = stringLiteral >>= return . c
+pString c = identifier >>= return . c
hunk ./src/UI/UIStarter.hs 501
-pNames = liftM ((map mkName) .  words) $ stringLiteral
+pNames = liftM ((map mkName) .  words) $ identifier
hunk ./src/UI/UIStarter.hs 505
-           ; bgkldg  <- choice [ do{eof; return []}
-                               , do{reservedOp "with"; pNames}
-                               ]
+           ; bgkldg  <- liftM (maybe [] id) $ optionMaybe (do{reservedOp "with"; pNames})