Add cmd line parsing

This commit is contained in:
Rnhmjoj 2014-11-21 00:14:57 +01:00
parent 1bb362651c
commit c663cb0d3c

36
Main.hs
View File

@ -1,10 +1,34 @@
import Network.Wreq
{-# LANGUAGE RecordWildCards #-}
import Network.Wreq (get, responseBody)
import System.Console.ArgParser
import Control.Lens
import Data.Aeson
import Json
main = do
req <- get "http://namecoin.dns:8088/id/rnhmjoj"
case decode (req ^. responseBody) of
Just res -> putStrLn $ repr res 0
Nothing -> putStrLn "Error parsing data"
data Name = Name
{ name :: String
, url :: String
, local :: Bool
, raw :: Bool
} deriving (Show)
parser :: ParserSpec Name
parser = Name
`parsedBy` reqPos "name" `Descr` "Namecoin name id"
`andBy` optPos "http://dns.dnschain.net/" "url" `Descr` "Use custom dnschain API url"
`andBy` boolFlag "local" `Descr` "Use local namecoind db"
`andBy` boolFlag "raw" `Descr` "Print raw JSON data"
main :: IO ()
main = mkApp parser >>= flip runApp exec
exec :: Name -> IO ()
exec args@Name{..} =
if local then putStrLn "to do"
else do
req <- get (url ++ name)
if raw then print $ req ^. responseBody
else case decode (req ^. responseBody) of
Just res -> putStrLn $ repr res
Nothing -> putStrLn "Error parsing data"