8

I have used the libraries criterion and cmdargs.

When I compile the program completely without cmdargs and run it e.g. ./prog --help then I get some unwanted response from criterion about the possible options and the number of runs etc..

When I compile and run it as below the command line options are first picked up by my code then then read by criterion. Criterion then subsequently reports and error telling me that the option --byte is unknown. I have not seen anything in the criterion documentation how this could be switched off or worked around. Is there a way to clear out the command line options ofter I have read them? Otherwise I would need to use e.g. CPUTime instead of criterion, that is OK to me since I do to really require the loads of extra functionality and data that criterion delivers.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable #-}

import System.Console.CmdArgs

data Strlen = Strlen {byte :: Int} deriving (Data, Typeable, Show)

strlen = cmdArgsMode $ Strlen {byte = def} &= summary "MessagePack benchmark v0.04"

main = do
  n <- cmdArgsRun strlen
  let datastring = take (byte n) $ randomRs ('a','z') (mkStdGen 3)
  putStrLn "Starting..."
  conn <- connect "192.168.35.62" 8081
  defaultMain [bench "sendReceive" $ whnfIO (mywl conn datastring)] 
hammar
  • 138,522
  • 17
  • 304
  • 385
J Fritsch
  • 3,338
  • 1
  • 18
  • 40

2 Answers2

12

Use System.Environment.withArgs. Parse the command line arguments first with cmdArgs, then pass what you haven't used to criterion:

main = do
    (flags, remaining) <- parseArgsHowever
    act according to flags
    withArgs remaining $
        defaultMain [ ... ]
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • I am afraid I do not understand how to apply this to my code snippet. – J Fritsch Dec 08 '11 at 19:23
  • @JFritsch Okay, I've just looked at the cmdargs docs, if I understdand them right, you'd have to add a field for other arguments to your type (`Strlen`) and collect them there with `args`. However, I'm not sure what cmdargs does with unknown options, which criterion arguments would be. If you want criterion to just use the default settings, `withArgs [] $ defaultMain ...` is simple and works. If you want to pass fixed args to criterion, `withArgs ["-s","200"]` e.g. – Daniel Fischer Dec 08 '11 at 20:30
  • Works of course if I import System.Environment ( withArgs ). I wonder if there is anything more elegant that keeps the resulting binary lean. – J Fritsch Dec 08 '11 at 21:08
3

Take a look at the criterion source. You should be able to write your own defaultMainWith function that handles args however you want, including ignoring them, or ignoring unknown args, or etc...

sclv
  • 38,665
  • 7
  • 99
  • 204