Created
July 6, 2025 16:32
-
-
Save kayvank/be68328327ef1c044b35b48604e6b5ab to your computer and use it in GitHub Desktop.
Revisions
-
kayvank created this gist
Jul 6, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,72 @@ {- | CLI parsing and budgetting execution time -} {-# LANGUAGE LambdaCase #-} {- CLI: --simulation-name ping-pong --number-seconds 45 -} import Control.Concurrent.Async (async, race) import Control.Concurrent (myThreadId, threadDelay) import Control.Monad (forever) import Control.Exception (finally) import Options.Applicative (Parser, ParserInfo, execParser, info, helper, metavar, option, progDesc , header, fullDesc, progDesc, showDefault , value, help, long, auto, strOption ,(<**>)) f1 :: String -> -- ^ simulation name Int -> -- ^ default delay IO () f1 name delay = h1 `finally` h2 where h1 = do mythreadid <- myThreadId forever $ do putStrLn $ "simulation name: " <> name <> "mythreadid: " <> show mythreadid <> " delay-secs: " <> show delay threadDelay delay h2 = putStrLn $ "finally invoded on f1.h2" data Cli = Cli { simulationName :: String , simulationExecSeconds :: Int } deriving (Show) cli :: Parser Cli cli = Cli <$> strOption ( long "simulation-name" <> metavar "SIMULATION" <> help "simulation to execute, options are `ping-pong` `run-node` `run-pure`" ) <*> option auto ( long "seconds" <> metavar "SIMULATION" <> help "number of seconds to run the simulation" <> showDefault <> value 15 ) cliOpts :: ParserInfo Cli cliOpts = info (cli <**> helper) ( fullDesc <> progDesc "Print a greeting for SIMULATION" <> header "iohk-interview assignment" ) main :: IO () main = do cliParams <- execParser cliOpts print cliParams e <- race (f1 (simulationName cliParams) 1_000_000) (threadDelay $ ((simulationExecSeconds cliParams ) * 1_000_000) ) print "hello"