Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Using the Generic
facility, this module can derive Parser
s automatically.
If you have a simple record:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
import Env
import Env.Generic
data Hello = Hello
{ name :: String
, count :: Int
, quiet :: Bool
} deriving (Show, Eq, Generic)
instance Record Error Hello
main :: IO ()
main = do
hello <- Env.parse (header "envparse example") record
print (hello :: Hello)
The generic implementation of the record
method translates named fields to field parsers:
% NAME=bob COUNT=3 runhaskell -isrc example/Generic0.hs Hello {name = "bob", count = 3, quiet = False}
If you want to adorn the ugly default help message, augment the fields with descriptions:
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeOperators #-} import Env import Env.Generic data Hello = Hello { name :: String ? "Whom shoud I greet?" , count :: Int ? "How many times to greet them?" , quiet :: Bool ? "Should I be quiet instead?" } deriving (Show, Eq, Generic) instance Record Error Hello main :: IO () main = do hello <- Env.parse (header "envparse example") record print (hello :: Hello)
% runhaskell -isrc example/Generic1.hs envparse example Available environment variables: COUNT How many times to greet them? NAME Whom shoud I greet? QUIET Should I be quiet instead? Parsing errors: COUNT is unset NAME is unset
Note that this has an effect of wrapping the values in the Help
constructor:
% NAME=bob COUNT=3 QUIET=YES
runhaskell -isrc example/Generic1.hs
Hello {name = Help {unHelp = "bob"}, count = Help {unHelp = 3}, quiet = Help {unHelp = True}}
Documentation
class Record e a where Source #
Given a Record e a
instance, a value of the type a
can be parsed from the environment.
If the parsing fails, a value of an error type e
is returned.
The record
method has a default implementation for any type that has a Generic
instance. If you
need to choose a concrete type for e
, the default error type Error
is a good candidate. Otherwise,
the features you'll use in your parsers will naturally guide GHC to compute the set of required
constraints on e
.
Nothing
class Field e a where Source #
Given a Field e a
instance, a value of the type a
can be parsed from an environment variable.
If the parsing fails, a value of an error type e
is returned.
The field
method has a default implementation for any type that has a Read
instance. If you
need to choose a concrete type for e
, the default error type Error
is a good candidate. Otherwise,
the features you'll use in your parsers will naturally guide GHC to compute the set of required
constraints on e
.
The annotated instances do not use the default implementation.
Nothing
Instances
A field annotation.
If you annotate a record field with a Symbol
literal (that is, a statically known type level string)
the derivation machinery will use the literal in the help message.
Please remember that the values of the annotated fields are wrapped in the Help
constructor.
Instances
(KnownSymbol tag, Field e a) => Field e (a ? tag) Source # | Augments the underlying field parser with the help message. |
Functor ((?) a :: Type -> Type) Source # | |
Foldable ((?) a :: Type -> Type) Source # | |
Defined in Env.Generic fold :: Monoid m => (a ? m) -> m # foldMap :: Monoid m => (a0 -> m) -> (a ? a0) -> m # foldMap' :: Monoid m => (a0 -> m) -> (a ? a0) -> m # foldr :: (a0 -> b -> b) -> b -> (a ? a0) -> b # foldr' :: (a0 -> b -> b) -> b -> (a ? a0) -> b # foldl :: (b -> a0 -> b) -> b -> (a ? a0) -> b # foldl' :: (b -> a0 -> b) -> b -> (a ? a0) -> b # foldr1 :: (a0 -> a0 -> a0) -> (a ? a0) -> a0 # foldl1 :: (a0 -> a0 -> a0) -> (a ? a0) -> a0 # elem :: Eq a0 => a0 -> (a ? a0) -> Bool # maximum :: Ord a0 => (a ? a0) -> a0 # minimum :: Ord a0 => (a ? a0) -> a0 # | |
Traversable ((?) a :: Type -> Type) Source # | |
Eq a => Eq (a ? tag) Source # | |
Show a => Show (a ? tag) Source # | |
Representable types of kind *
.
This class is derivable in GHC with the DeriveGeneric
flag on.
A Generic
instance must satisfy the following laws:
from
.to
≡id
to
.from
≡id
Instances
Generic Bool | Since: base-4.6.0.0 |
Generic Ordering | Since: base-4.6.0.0 |
Generic () | Since: base-4.6.0.0 |
Generic ExitCode | |
Generic All | Since: base-4.7.0.0 |
Generic Any | Since: base-4.7.0.0 |
Generic Fixity | Since: base-4.7.0.0 |
Generic Associativity | Since: base-4.7.0.0 |
Defined in GHC.Generics type Rep Associativity :: Type -> Type # from :: Associativity -> Rep Associativity x # to :: Rep Associativity x -> Associativity # | |
Generic SourceUnpackedness | Since: base-4.9.0.0 |
Defined in GHC.Generics type Rep SourceUnpackedness :: Type -> Type # from :: SourceUnpackedness -> Rep SourceUnpackedness x # to :: Rep SourceUnpackedness x -> SourceUnpackedness # | |
Generic SourceStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics type Rep SourceStrictness :: Type -> Type # from :: SourceStrictness -> Rep SourceStrictness x # to :: Rep SourceStrictness x -> SourceStrictness # | |
Generic DecidedStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics type Rep DecidedStrictness :: Type -> Type # from :: DecidedStrictness -> Rep DecidedStrictness x # to :: Rep DecidedStrictness x -> DecidedStrictness # | |
Generic [a] | Since: base-4.6.0.0 |
Generic (Maybe a) | Since: base-4.6.0.0 |
Generic (Par1 p) | Since: base-4.7.0.0 |
Generic (Min a) | Since: base-4.9.0.0 |
Generic (Max a) | Since: base-4.9.0.0 |
Generic (First a) | Since: base-4.9.0.0 |
Generic (Last a) | Since: base-4.9.0.0 |
Generic (WrappedMonoid m) | Since: base-4.9.0.0 |
Defined in Data.Semigroup type Rep (WrappedMonoid m) :: Type -> Type # from :: WrappedMonoid m -> Rep (WrappedMonoid m) x # to :: Rep (WrappedMonoid m) x -> WrappedMonoid m # | |
Generic (Option a) | Since: base-4.9.0.0 |
Generic (ZipList a) | Since: base-4.7.0.0 |
Generic (Identity a) | Since: base-4.8.0.0 |
Generic (First a) | Since: base-4.7.0.0 |
Generic (Last a) | Since: base-4.7.0.0 |
Generic (Dual a) | Since: base-4.7.0.0 |
Generic (Endo a) | Since: base-4.7.0.0 |
Generic (Sum a) | Since: base-4.7.0.0 |
Generic (Product a) | Since: base-4.7.0.0 |
Generic (Down a) | Since: base-4.12.0.0 |
Generic (NonEmpty a) | Since: base-4.6.0.0 |
Generic (Either a b) | Since: base-4.6.0.0 |
Generic (V1 p) | Since: base-4.9.0.0 |
Generic (U1 p) | Since: base-4.7.0.0 |
Generic (a, b) | Since: base-4.6.0.0 |
Generic (Arg a b) | Since: base-4.9.0.0 |
Generic (WrappedMonad m a) | Since: base-4.7.0.0 |
Defined in Control.Applicative type Rep (WrappedMonad m a) :: Type -> Type # from :: WrappedMonad m a -> Rep (WrappedMonad m a) x # to :: Rep (WrappedMonad m a) x -> WrappedMonad m a # | |
Generic (Proxy t) | Since: base-4.6.0.0 |
Generic (Rec1 f p) | Since: base-4.7.0.0 |
Generic (URec (Ptr ()) p) | Since: base-4.9.0.0 |
Generic (URec Char p) | Since: base-4.9.0.0 |
Generic (URec Double p) | Since: base-4.9.0.0 |
Generic (URec Float p) | |
Generic (URec Int p) | Since: base-4.9.0.0 |
Generic (URec Word p) | Since: base-4.9.0.0 |
Generic (a, b, c) | Since: base-4.6.0.0 |
Generic (WrappedArrow a b c) | Since: base-4.7.0.0 |
Defined in Control.Applicative type Rep (WrappedArrow a b c) :: Type -> Type # from :: WrappedArrow a b c -> Rep (WrappedArrow a b c) x # to :: Rep (WrappedArrow a b c) x -> WrappedArrow a b c # | |
Generic (Kleisli m a b) | Since: base-4.14.0.0 |
Generic (Const a b) | Since: base-4.9.0.0 |
Generic (Ap f a) | Since: base-4.12.0.0 |
Generic (Alt f a) | Since: base-4.8.0.0 |
Generic (K1 i c p) | Since: base-4.7.0.0 |
Generic ((f :+: g) p) | Since: base-4.7.0.0 |
Generic ((f :*: g) p) | Since: base-4.7.0.0 |
Generic (a, b, c, d) | Since: base-4.6.0.0 |
Generic (M1 i c f p) | Since: base-4.7.0.0 |
Generic ((f :.: g) p) | Since: base-4.7.0.0 |
Generic (a, b, c, d, e) | Since: base-4.6.0.0 |
Generic (a, b, c, d, e, f) | Since: base-4.6.0.0 |
Generic (a, b, c, d, e, f, g) | Since: base-4.6.0.0 |