Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Here's a simple example of a program that uses envparse
's parser:
module Main (main) where import Control.Monad (unless) import Env data Hello = Hello { name :: String, quiet :: Bool } hello :: IO Hello hello = Env.parse
(header
"envparse example") $ Hello <$>var
(str
<=<nonempty
) "NAME" (help
"Target for the greeting") <*>switch
"QUIET" (help
"Whether to actually print the greeting") main :: IO () main = do Hello { name, quiet } <- hello unless quiet $ putStrLn ("Hello, " ++ name ++ "!")
The NAME
environment variable is mandatory and contains the name of the person to
greet. QUIET
, on the other hand, is an optional boolean flag, false by default, that
decides whether the greeting should be silent.
If the NAME
variable is undefined in the environment then running the program will
result in the following help text:
envparse example Available environment variables: NAME Target for the greeting QUIET Whether to actually print the greeting Parsing errors: NAME is missing
- parse :: Mod Info a -> Parser a -> IO a
- data Parser a
- data Mod t a
- data Info a
- header :: String -> Mod Info a
- desc :: String -> Mod Info a
- footer :: String -> Mod Info a
- var :: Reader a -> String -> Mod Var a -> Parser a
- data Var a
- type Reader a = String -> Either String a
- str :: IsString s => Reader s
- nonempty :: IsString s => Reader s
- auto :: Read a => Reader a
- def :: a -> Mod Var a
- helpDef :: (a -> String) -> Mod Var a
- flag :: a -> a -> String -> Mod Flag a -> Parser a
- switch :: String -> Mod Flag Bool -> Parser Bool
- data Flag a
- class HasHelp t
- help :: HasHelp t => String -> Mod t a
- pure :: Applicative f => forall a. a -> f a
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<*>) :: Applicative f => forall a b. f (a -> b) -> f a -> f b
- (*>) :: Applicative f => forall a b. f a -> f b -> f b
- (<*) :: Applicative f => forall a b. f a -> f b -> f a
- optional :: Alternative f => f a -> f (Maybe a)
- empty :: Alternative f => forall a. f a
- (<|>) :: Alternative f => forall a. f a -> f a -> f a
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- (<>) :: Monoid m => m -> m -> m
- mempty :: Monoid a => a
- mconcat :: Monoid a => [a] -> a
- asum :: (Foldable t, Alternative f) => t (f a) -> f a
- parseTest :: Parser a -> [(String, String)] -> Maybe a
Documentation
header :: String -> Mod Info a Source
A help text header (it usually includes an application name and version)
type Reader a = String -> Either String a Source
An environment variable's value parser. Use (<=<)
and (>=>)
to combine these
The default value of the variable
Note: specifying it means the parser won't ever fail.
A flag that takes the active value if the environment variable is set and non-empty and the default value otherwise
Note: this parser never fails.
switch :: String -> Mod Flag Bool -> Parser Bool Source
A simple boolean flag
Note: the same caveats apply.
A class of things that can have a help message attached to them
setHelp
Re-exports
External functions that may be useful to the consumer of the library
pure :: Applicative f => forall a. a -> f a
Lift a value.
(<*>) :: Applicative f => forall a b. f (a -> b) -> f a -> f b
Sequential application.
(*>) :: Applicative f => forall a b. f a -> f b -> f b
Sequence actions, discarding the value of the first argument.
(<*) :: Applicative f => forall a b. f a -> f b -> f a
Sequence actions, discarding the value of the second argument.
optional :: Alternative f => f a -> f (Maybe a)
One or none.
empty :: Alternative f => forall a. f a
The identity of <|>
(<|>) :: Alternative f => forall a. f a -> f a -> f a
An associative binary operation
(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c infixr 1
Right-to-left Kleisli composition of monads. (
, with the arguments flipped>=>
)
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1
Left-to-right Kleisli composition of monads.
mconcat :: Monoid a => [a] -> a
Fold a list using the monoid.
For most types, the default definition for mconcat
will be
used, but the function is included in the class definition so
that an optimized version can be provided for specific types.
asum :: (Foldable t, Alternative f) => t (f a) -> f a
The sum of a collection of actions, generalizing concat
.
Testing
Utilities to test—without dabbling in IO—that your parsers do what you want them to do