From a4f4fb22252e6449d2e2e951edbd7679d54c6c8d Mon Sep 17 00:00:00 2001
From: Hans-Peter Deifel <hpd@hpdeifel.de>
Date: Tue, 18 Oct 2016 11:15:22 +0200
Subject: [PATCH] Clean up command line argument handling

Now uses optparse-applicative. This will allow to easily add new
parameters in the future.
---
 osek-verification.cabal |  1 +
 src/main/Main.hs        | 35 +++++++++++++++++++++++++----------
 2 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/osek-verification.cabal b/osek-verification.cabal
index 114370c..8935e78 100644
--- a/osek-verification.cabal
+++ b/osek-verification.cabal
@@ -42,6 +42,7 @@ executable mockup_generator
                      , text >= 1.2 && <1.3
                      , bytestring >= 0.10 && <0.11
                      , osek-verification
+                     , optparse-applicative >= 0.12 && <0.13
   hs-source-dirs:      src/main
   default-language:    Haskell2010
 
diff --git a/src/main/Main.hs b/src/main/Main.hs
index bc78bc5..0dc1e3c 100644
--- a/src/main/Main.hs
+++ b/src/main/Main.hs
@@ -4,7 +4,10 @@ module Main where
 
 import qualified Data.ByteString.Lazy as BS
 import qualified Data.Text.IO as T
-import System.Environment
+import qualified Data.Text as T
+import           Data.Text (Text)
+import           System.Environment
+import           Options.Applicative
 
 import qualified CFG.Parser as P
 import CFG.Graph
@@ -14,17 +17,29 @@ import CFG.Sanitize
 import Search.States2Dot
 import Search.Search
 
-main :: IO ()
-main = do
-  getArgs >>= \case
-    ["graph"] -> do
-      cntnt <- T.getContents
-      T.putStrLn $ states2Dot cntnt
+data Command = Mockup | Search SearchOpts
+
+optParser :: Parser Command
+optParser = subparser
+  (  command "mockup" (info (helper <*> pure Mockup) (progDesc "Generate mockup"))
+  <> command "search" (info (helper <*> (Search <$> searchOptParser))
+                       (progDesc "Extract CFG by running EXECUTABLE"))
+  )
 
-    ["search", prog] -> do
-      driver prog
+data SearchOpts = SearchOpts
+  { mockupExecutable :: FilePath
+  }
 
-    _         -> do
+searchOptParser :: Parser SearchOpts
+searchOptParser = SearchOpts <$> argument str (metavar "EXECUTABLE")
+
+main :: IO ()
+main = do
+  cmd <- execParser $ info (helper <*> optParser)
+            ( fullDesc <> progDesc "dOSEK verification tool" )
+  case cmd of
+    (Search opts) -> driver (mockupExecutable opts)
+    Mockup        -> do
       cntnt <- BS.getContents
       case P.parseFile cntnt of
         Left err -> putStrLn err
-- 
GitLab