Vega512
Vega512

Reputation: 321

haskell getArgs

How does getArgs work, I am trying to read in a text file from the terminal which then sends the contents to one of my defined functions. Can someone post an example code to give me an idea of how it works. Thanks.

Upvotes: 3

Views: 3432

Answers (2)

MathematicalOrchid
MathematicalOrchid

Reputation: 62818

getArgs gives you a list of command-line arguments (not including what C programmers call argv[0], the name of the running binary). So for example, if you compiled your application as foo and then executed foo a b c on the command-line, then getArgs would return ["a", "b", "c"].

From your question, I'm not sure whether you're expecting a filename on the command-line, or whether you're expecting to pipe the data in on standard-in.

If you're after a filename, just take the result from getArgs and pass it to openFile (or perhaps readFile, depending on what you're after).

If you're trying to do piping, you might look at interact, which may do what you want.

Upvotes: 4

singpolyma
singpolyma

Reputation: 11241

getArgs is an IO action which produces a list of String

fmap someFunction $ readFile =<< fmap head getArgs

or in Applicative style:

someFunction <$> readFile =<< head <$> getArgs

Upvotes: 11

Related Questions