diff --git a/src/Options/Applicative.hs b/src/Options/Applicative.hs index 4aba6038..c21f1bf9 100644 --- a/src/Options/Applicative.hs +++ b/src/Options/Applicative.hs @@ -130,6 +130,8 @@ module Options.Applicative ( auto, str, + integral, + numeric, maybeReader, eitherReader, disabled, diff --git a/src/Options/Applicative/Builder.hs b/src/Options/Applicative/Builder.hs index 06ebfc55..e0c24f81 100644 --- a/src/Options/Applicative/Builder.hs +++ b/src/Options/Applicative/Builder.hs @@ -55,6 +55,8 @@ module Options.Applicative.Builder ( -- | A collection of basic 'Option' readers. auto, str, + integral, + numeric, maybeReader, eitherReader, disabled, @@ -118,9 +120,14 @@ import Options.Applicative.Help.Chunk -- | 'Option' reader based on the 'Read' type class. auto :: Read a => ReadM a -auto = eitherReader $ \arg -> case reads arg of - [(r, "")] -> return r - _ -> Left $ "cannot parse value `" ++ arg ++ "'" +auto = + eitherReader $ \arg -> + case reads arg of + [(r, "")] -> + return r + _ -> + Left $ "cannot parse value `" ++ arg ++ "'" + -- | String 'Option' reader. -- @@ -128,6 +135,33 @@ auto = eitherReader $ \arg -> case reads arg of str :: IsString s => ReadM s str = fromString <$> readerAsk + +-- | Integral 'Option' reader. +-- +-- Safe ReadM for integral types. +integral :: Integral i => ReadM i +integral = + eitherReader $ \arg -> + case reads arg of + [(r, "")] -> + Right $ fromInteger r + _ -> + Left $ "cannot parse value `" ++ arg ++ "' as integral type" + + +-- | Numeric 'Option' reader. +-- +-- Safe ReadM for numbers. +numeric :: (Read n, Num n) => ReadM n +numeric = + eitherReader $ \arg -> + case reads arg of + [(r, "")] -> + Right r + _ -> + Left $ "cannot parse `" ++ arg ++ "' as a number" + + -- | Convert a function producing an 'Either' into a reader. -- -- As an example, one can create a ReadM from an attoparsec Parser