module Text.LambdaOptions.Parseable (
Parseable(..),
) where
import Data.Word
import Text.Read
import Text.Read.Bounded
class Parseable a where
parse :: [String] -> (Maybe a, Int)
simpleParse :: (String -> Maybe a) -> [String] -> (Maybe a, Int)
simpleParse parser args = case args of
[] -> (Nothing, 0)
s : _ -> case parser s of
Nothing -> (Nothing, 0)
Just x -> (Just x, 1)
parseBounded :: (ReadBounded a) => [String] -> (Maybe a, Int)
parseBounded = simpleParse $ \str -> case readBounded str of
NoRead -> Nothing
ClampedRead _ -> Nothing
ExactRead x -> Just x
instance Parseable Word where
parse = parseBounded
instance Parseable Int where
parse = parseBounded
instance Parseable Integer where
parse = parseBounded
instance Parseable String where
parse = simpleParse Just
instance Parseable Float where
parse = simpleParse readMaybe
instance (Parseable a) => Parseable (Maybe a) where
parse args = case parse args of
(Nothing, n) -> (Just Nothing, n)
(Just x, n) -> (Just $ Just x, n)