-
-
Save jmn/fccd440852a124c38530799aac68581c to your computer and use it in GitHub Desktop.
Haskell CmdArgs optional positional arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE DeriveDataTypeable #-} | |
module Main where | |
import Lib | |
import System.Environment (getArgs) | |
import Database.SQLite.Simple | |
import System.Console.CmdArgs | |
import Data.List | |
data Options = Options { | |
inputMode :: Bool, | |
message :: String | |
} deriving (Data, Typeable) | |
options :: Options | |
options = Options { inputMode = False | |
&= typ "Input mode" | |
&= help "Toggle input mode", | |
message = def | |
&= typ "message" | |
&= opt ("" :: String) | |
&= args | |
} | |
&= summary "n notes" | |
&= program "n" | |
data TestField = TestField Int String deriving (Show) | |
instance FromRow TestField where | |
fromRow = TestField <$> field <*> field | |
instance ToRow TestField where | |
toRow (TestField id_ str) = toRow (id_, str) | |
dbPut conn val = | |
execute conn "INSERT INTO test (str) VALUES (?)" (Only (val :: String)) | |
main :: IO () | |
main = do | |
opts <- cmdArgs options | |
conn <- open "test.db" | |
execute_ conn "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, str TEXT)" | |
case (message opts) of | |
"" -> return () | |
x -> dbPut conn x | |
case (inputMode opts) of | |
True -> do | |
content <- getContents | |
dbPut conn content | |
False -> | |
return () | |
r <- query_ conn "SELECT * from test" :: IO [TestField] | |
mapM_ print r | |
close conn | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This program behaves weird when running in GHCi but seems to work fine when compiled.