I'm using getArgs
in my Haskell program to parse arguments from the command line. However, I've noticed that I have problems when I call my program like this:
runhaskell Main *.hs .
As you can imagine, Main
looks at *.hs
files in the .
directory.
But there's a problem. The code below doesn't behave as I'd expect.
import System
main :: IO ()
main = do
args <- getArgs
print args
I want to see
args = ["*.hs","."]
but instead I see
args = ["file1.hs","file2.hs","file3.hs","."]
My program needs to expand the glob expression itself because it does it in multiple directories. But how can I have getArgs
return the raw list of arguments instead of trying to be magical and do the parsing itself?
getArgs doesn't parse the glob expressison - getArgs never sees the glob expression. The glob expression is parsed and expanded by your shell before your program even starts.
There's nothing you can do inside your program to prevent this - your only option is to escape the glob in the shell (by adding a backslash before the *
for example or surrounding the argument with single quotes).
This isn't haskell, this is your shell.
Normally file globs (like *.hs
) are interpreted by the shell before the command is passed to haskell.
To see this try running
runhaskell Main '*.hs' .
The single quotes will prevent your shell from interpreting the glob, and pass the argument as-is to runhaskell
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With