Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use getContents to take input from the command line?

Tags:

haskell

My program allows the user to specify a file which is read as input, however this is optional. If the user does not specify a file, I'd like to read input in from the command line.

I have this so far:

main :: IO()
main = do
  (opts, mbArgs) <- parseCmdLine
  input <-
    case mbArgs of
      Nothing   -> getContents
      Just file -> readFile file

This doesn't seem to be working. When a user doesn't stipulate a file, they are able to enter input, but there seems to be no way of terminating so that the program can then work on that input.

I thought that you had to press Ctrl+D, but that doesn't do anything.

Thanks for any help.

like image 977
Nick Brunt Avatar asked Jan 17 '23 08:01

Nick Brunt


1 Answers

In a typical Unix-like terminal (such as Cygwin, at least in Cygwin's rxvt; not sure about the Windows Command Prompt), a Ctrl+D only sends EOF when you're at the start of a line. If you hit Enter and then Ctrl+D, it should work. If you want to send EOF without a newline, hit Ctrl+D twice in a row.

If it's not that, then there's presumably some other problem with your terminal; the code looks fine.

like image 154
ehird Avatar answered Feb 01 '23 07:02

ehird