I need to get program arguments and show them
module Main ( main ) where
import System ( getArgs )
main = do
args<-getArgs
print $ show args
But it does nothing.Maybe my call incorrect?
>main 3 4
It sounds like you're trying to run the program from within GHCi. In that case, you can use the :main command to run your program with arguments.
*Main> :main foo bar
"[\"foo\",\"bar\"]"
Try changing 'System' -> 'System.Environment':
module Main ( main ) where
import System.Environment ( getArgs )
main = do
args <- getArgs
mapM putStrLn args
Related
I am very new to Haskell.
I am trying to use Criterion to get performance data. My Main module is as follows:
module Main where
import SingleThreadedBlockChain
import Data.Time.Clock.System (getSystemTime)
import System.IO (readFile)
import System.TimeIt
import System.Environment ( getArgs )
import Criterion.Main
main :: IO ()
main = do
args <- getArgs
time <- getSystemTime
content <- readFile (args !! 0)
defaultMain [
bench "putStrLn" $ nfIO (putStrLn ("The last block is " ++ show (last (makeBlockChain "" (lines content) 0 (show time)))))
]
I am trying to use the Criterion documentation + things I've seen on StackOverflow to get this working. I'm getting the following error:
Error: none of the specified names matches a benchmark
I thought I would be benchmarking IO. From the examples I've seen, the names don't always match the benchmarks. Could someone explain how the names should relate to the benchmarks?
Criterion's defaultMain does its own cli argument parsing. It uses the first argument to try and match a specific benchmark name to run (in your case you only have "putStrLn"). If you want to do your own argument parsing you can change the arguments before passing to defaultMain like this:
args <- getArgs
withArgs (drop 1 args) $ defaultMain ...
This will hide the first argument from defaultMain so you can use it as you please.
I am trying to understand getArgs but I am getting a weird behavior that I am not understanding. Here is my program:
getMyArgs :: IO [String]
getMyArgs =do
x <- getArgs
return x
I run this and get:
*Main> hello <- getMyArgs
*Main>
Why doesn't it return my argument passed? I tried to put in a " show() " but that turns it into a String instead of a [String]
getMyArgs :: IO [String]
getMyArgs =do
x <- getArgs
return x
The do notation desugars to:
getMyArgs :: IO [String]
getMyArgs = getArgs >>= \x -> return x
Using the right identity we can rewrite this to:
getMyArgs :: IO [String]
getMyArgs = getArgs
So you've just defined a new name for getArgs. Now why does getArgs not show your program arguments? Well it appears you didn't provide any program arguments. In the interpreter it can be tricky to provide arguments - one way is to :set them:
Prelude> :set args hello world
Prelude> import System.Environment
Prelude System.Environment> getArgs
["hello","world"]
EDIT: Oh you might be looking to print the value you bound. Consider:
Prelude System.Environment> hello <- getArgs
Prelude System.Environment> print hello
["hello","world"]
Thanks to #4castle for this observation.
Assume your Haskell program is compiled to an executable foo. When you call your program, you want to pass some runtime arguments to your program eg foo param1 param2 . Depending on the values of param1 and param2 you will take different actions in your program.
Now with the getArgs function you get access to these parameters in your Haskell program.
In GHCi this argument passing can be simulated. Either with the :set args paarm1 param2 command as shown in the answer of Thomas M. DuBuisson
or you call your main program in GHCI with :main param1 param2 .
In both scenarios getEnv will return IO ["param1", "param2"]
Here I have text file, I want to load it in Haskell, and read it in a List.
import qualified System.Environment
main :: IO ()
main = do
[path] <- System.Environment.getArgs
g <- readFile path
putStr g
But I don't know how to give the address. (Example: the file is /Users/Documents/Programming/test.txt)
After I load this Haskell file:
*Main> main -- How can I write the address?
Under :help, you can see documentation for the :main command:
:main [<arguments> ...] run the main function with the given arguments
Example:
ghci> :main /Users/Documents/Programming/test.txt
...
contents of test.txt
...
I am attempting to run my program using the command line. I am trying to return my command line arguments:
import System.Environment
import Data.List
main :: IO()
main = do
args <- getArgs
progName <- getProgName
putStrLn "The arguments are:"
mapM putStrLn args
putStrLn "The program name is:"
putStrLn progName
I am executing the code by calling the main function with my arguments:
main argument arguments "more arguements"
However, I am getting a complier error:
<interactive>:33:6: Not in scope: ‘argument’
<interactive>:33:15: Not in scope: ‘arguments’
Is there an issue with how I am calling my function with my arguments?
You have to use :main if you want to simulate command line arguments. main alone only executes your IO () action, but doesn't actually build the arguments. For all what GHCi knows, main doesn't necessarily need to be IO (), it could be Int -> Int -> IO ().
However, if you use :main, GHC will use main in the same way it would get invoked during an runhaskell call, e.g. with interpreting the following parameters as command line arguments.
Alternatively, you can use withArgs from System.Environment:
ghci> withArgs ["argument", "arguments", "more arguments"] main
I'm learning haskell and decided to try writing some small test programs to get use to Haskell code and using modules. Currently I'm trying to use the first argument to create a password hash using the Cypto.PasswordStore. To test out my program I'm trying to create a hash from the first argument and then print the hash to screen.
import Crypto.PasswordStore
import System.Environment
main = do
args <- getArgs
putStrLn (makePassword (head args) 12)
I'm getting the following error:
testmakePassword.hs:8:19:
Couldn't match expected type `String'
with actual type `IO Data.ByteString.Internal.ByteString'
In the return type of a call of `makePassword'
In the first argument of `putStrLn', namely
`(makePassword (head args) 12)'
In a stmt of a 'do' block: putStrLn (makePassword (head args) 12)
I've been using the following links as references but I am now just trial-erroring to no avail.
http://hackage.haskell.org/packages/archive/bytestring/0.9.0.4/doc/html/Data-ByteString-Internal.html
http://hackage.haskell.org/packages/archive/pwstore-purehaskell/2.1/doc/html/Crypto-PasswordStore.html
You haven't imported ByteString, so it's trying to use the String version of putStrLn.
I've provided toBS for the String->ByteString conversion.
Try
import Crypto.PasswordStore
import System.Environment
import qualified Data.ByteString.Char8 as B
toBS = B.pack
main = do
args <- getArgs
makePassword (toBS (head args)) 12 >>= B.putStrLn
You have to do two things differently. First, makePassword is in IO, so you need to bind the result to a name and then pass the name to the IO function. Secondly, you need to import IO functions from Data.ByteString
import Crypto.PasswordStore
import System.Environment
import qualified Data.ByteString as B
main = do
args <- getArgs
pwd <- makePassword (B.pack $ head args) 12
B.putStrLn pwd
Or, if you won't be using the password result anywhere else, you can use bind to connect the two functions directly:
main = do
args <- getArgs
B.putStrLn =<< makePassword (B.pack $ head args) 12