Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, just dipping my toes into the waters of Haskell and need some help parsing command line args ( yes I've googled ) the code below works perfectly but I would like to pass a value on the command line - there is a Haskell function called getArgs that returns a string list of all command line params but I can't work out how to retrieve an int from the list - where I say do query 3400 I would like to say ( as in C ) query atoi(arg[1]) - any Haskellers out there ?

F#
import Database.HDBC.Sqlite3
import Database.HDBC
import System.Environment

main :: IO()
main =
     do query 3400


query :: Int -> IO ()
query id =
    do conn <- connectSqlite3 "library.db"
       r <- quickQuery' conn
            "SELECT id, name from contributors where id = ? ORDER BY id" [toSql id]

       -- Convert each row into a String
       let stringRows = map convRow r

       -- Print the rows out
       mapM_ putStrLn stringRows

       -- And disconnect from the database
       disconnect conn

    where convRow :: [SqlValue] -> String
          convRow [sqlid, sqlname] =
              show id ++ ": " ++ name
              where id = (fromSql sqlid)::Integer
                    name = case fromSql sqlname of
                             Just x -> x
                             Nothing -> "NULL"
          convRow x = fail $ "Unexpected result: " ++ show x
Posted

I am rusty at Haskell, however, I suppose
main = do
  args <- getArgs
  query  (read (args!!1) :: Int)


should do the trick.
 
Share this answer
 
I was just about to post this which seems to work

F#
arg1 <- getArgs
 query (read $ head arg1)


But thanks I'll try your way to.

Edit

I had to change !! 1 to !! 0 and then it worked - thanks a lot
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900