Add cmd line parsing
This commit is contained in:
parent
1bb362651c
commit
c663cb0d3c
34
Main.hs
34
Main.hs
@ -1,10 +1,34 @@
|
|||||||
import Network.Wreq
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
|
||||||
|
import Network.Wreq (get, responseBody)
|
||||||
|
import System.Console.ArgParser
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Json
|
import Json
|
||||||
|
|
||||||
main = do
|
data Name = Name
|
||||||
req <- get "http://namecoin.dns:8088/id/rnhmjoj"
|
{ name :: String
|
||||||
case decode (req ^. responseBody) of
|
, url :: String
|
||||||
Just res -> putStrLn $ repr res 0
|
, 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"
|
Nothing -> putStrLn "Error parsing data"
|
||||||
|
Loading…
Reference in New Issue
Block a user