Ben
Ben

Reputation: 215

WxHaskell breaks command line arg functionality

I've written a program in WXHaskell, and verified that it works. Long story short, I planned for the program to be usable via GUI or by command line interface. I later went on to add command line argument processing with the GetOpts library and began testing that the various arguments worked as intended.

However, once I began running the program with command line options, the program began failing: any time the program was invoked in GUI mode (i.e. running WXHaskell) the program crashed with a warning about unrecognized command line options (this warning was in the form of a WXwidgets popup window). Anytime the program was run in terminal mode, all the options were processed correctly, so my suspicion is that running WX computations causes the "ARGV" values to be passed to some WX function down the line, and of course since these args are intended for the main program body, they will be unrecognized.

I've written a small test-case that reproduces this behavior (without GetOpts).

import Graphics.UI.WX
import System.Environment (getArgs)

gui = do
  fr <- frame [text := "GUI calculation"]
  t <- staticText fr [text := "Enter a number below"]
  n <- entry fr []
  let foo = do
        v <- read <$> get n text
        set t [text := "Your number * 3 is " ++ show (v * 3 :: Int) ]
  b <- button fr [text := "Calculate", on command := foo]
  set fr [layout := column 3 [widget t, widget n, widget b] ]

terminal = do
  putStrLn "Enter a number"
  v <- read <$> getLine
  putStrLn $ "Your number * 3 is " ++ (show $ v * 3)

main = do
  a <- getArgs
  case a of
    ("terminal"):_ -> terminal
    ("gui"):_ -> start gui
    _ -> start gui

Command line args are processed, and the computation (simply multiplying a user-specified Int by 3) is run either on the terminal or through a gui.

If the user specifies "terminal", the program completes without problem. If the user specifies no args the computation is run in gui mode by default, and also completes normally.

If the user specifies the "gui" argument, the program crashes with an error (in a popup window): "Unexpected parameter 'gui'".

If anyone has any idea why my args are apparently being passed on to subsequent operations, or better yet, how to stop this from happening, I'd be very happy!

Upvotes: 1

Views: 115

Answers (1)

phischu
phischu

Reputation: 440

I have experienced the same problem. My workaround was to use withArgs to start the GUI without any command line arguments like so:

main = do
  a <- getArgs
  case a of
    ("terminal"):_ -> terminal
    ("gui"):_ -> withArgs [] (start gui)
    _ -> withArgs [] (start gui)

Upvotes: 4

Related Questions