3-- Copyright : (c) 2019 Composewell Technologies
6-- Maintainer : streamly@composewell.com
8module Common (parseCLIOpts) where
10import Control.Exception (evaluate)
11import Control.Monad (when)
12import Data.List (scanl')
13import Data.Maybe (catMaybes)
14import System.Console.GetOpt
15 (OptDescr(..), ArgDescr(..), ArgOrder(..), getOpt')
16import System.Environment (getArgs, lookupEnv, setEnv)
17import Text.Read (readMaybe)
21-------------------------------------------------------------------------------
22-- Parse custom CLI options
23-------------------------------------------------------------------------------
25data BenchOpts = StreamSize Int deriving Show
27getStreamSize :: String -> Int
29 case (readMaybe size :: Maybe Int) of
31 Nothing -> error "Stream size must be numeric"
33options :: [OptDescr BenchOpts]
36 Option [] ["stream-size"] (ReqArg getSize "COUNT") "Stream element count"
41 getSize = StreamSize . getStreamSize
44 :: (Maybe String, Maybe String) -- (prev, yielded)
46 -> (Maybe String, Maybe String)
47deleteOptArgs (Nothing, _) opt =
48 if opt == "--stream-size"
49 then (Just opt, Nothing)
50 else (Just opt, Just opt)
52deleteOptArgs (Just prev, _) opt =
53 if opt == "--stream-size" || prev == "--stream-size"
54 then (Just opt, Nothing)
55 else (Just opt, Just opt)
57parseCLIOpts :: Int -> IO (Int, Config, [String])
58parseCLIOpts defaultStreamSize = do
61 -- Parse custom options
62 let (opts, _, _, errs) = getOpt' Permute options args
63 when (not $ null errs) $ error $ concat errs
64 (streamSize, args') <-
66 StreamSize x : _ -> do
67 -- When using the gauge "--measure-with" option we need to make
68 -- sure that we pass the stream size to child process forked by
69 -- gauge. So we use this env var for that purpose.
70 setEnv "STREAM_SIZE" (show x)
71 -- Hack! remove the option and its argument from args
72 -- getOpt should have a way to return the unconsumed args in
78 $ scanl' deleteOptArgs (Nothing, Nothing) args
81 r <- lookupEnv "STREAM_SIZE"
84 s <- evaluate $ getStreamSize x
86 Nothing -> return (defaultStreamSize, args)
88 -- Parse gauge options
89 let config = defaultConfig
92 , includeFirstIter = streamSize > defaultStreamSize
94 let (cfg, benches) = parseWith config args'
95 streamSize `seq` return (streamSize, cfg, benches)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list