Moduleenvparse-0.6.0Haskell2010
Env
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 unset
- 8 types
- 4 classes
- 28 values
- Packageenvparse-0.6.0
- Exports41
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceEnv.hs
Try to parse the environment
Use this if simply dying on failure (the behavior of parse) is inadequate for your needs.
An environment parser
Instances3Functor, Applicative, Alternative
Functor (Parser e)Defined in envparse-0.6.0 · Env.Internal.ParserApplicative (Parser e)Defined in envparse-0.6.0 · Env.Internal.ParserAlternative (Parser e)Defined in envparse-0.6.0 · Env.Internal.Parser
Parser's metadata
Set the help text header (it usually includes the application's name and version)
Set the short description
Set the max info width.
Note: It will be set to 26 columns if a smaller value is passed.
An error handler
Given a variable name and an error value, try to produce a useful error message
The default error handler
The string to prepend to the name of every declared environment variable
Environment variable metadata
An environment variable's value parser. Use (<=<) and (>=>) to combine these
Create a Reader from a simple parser function
The trivial reader
The single character string reader
The reader that accepts only non-empty strings
The reader that splits a string into a list of strings consuming the separator.
The reader that uses the Read instance of the type
The default value of the variable
Note: specifying it means the parser won't ever fail.
Show the default value of the variable in help.
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.
A simple boolean flag
Note: this parser never fails.
Flag metadata
A class of things that can have a help message attached to them
Attach help text to the variable
Mark the enclosed variables as sensitive to remove them from the environment once they've been parsed successfully.
A pretty-printed list of recognized environment variables suitable for usage messages
The type of errors returned by envparse's Readers. These fall into 3
categories:
Variables that are unset in the environment.
Variables whose value is empty.
Variables whose value cannot be parsed.
Constructors
Instances5Eq, Show, AsEmpty, AsUnread, AsUnset
The class of types that contain and can be constructed from the error returned from parsing unset variables.
The class of types that contain and can be constructed from the error returned from parsing variables whose value is empty.
The class of types that contain and can be constructed from the error returned from parsing variable whose value cannot be parsed.
Re-exports
5 declarationsExternal functions that may be useful to the consumer of the library
One or none.
It is useful for modelling any computation that is allowed to fail.
Examples
Using the Alternative instance of Control.Monad.Except, the following functions:
import Control.Monad.ExceptcanFail = throwError "it failed" :: Except String Intfinal = return 42 :: Except String Int
Can be combined by allowing the first function to fail:
runExcept $ canFail *> finalLeft "it failed"
runExcept $ optional canFail *> finalRight 42
An associative operation.
Examples
[1,2,3] <> [4,5,6][1,2,3,4,5,6]
Just [1, 2, 3] <> Just [4, 5, 6]Just [1,2,3,4,5,6]
putStr "Hello, " <> putStrLn "World!"Hello, World!
The sum of a collection of actions using (<|>), generalizing concat.
asum is just like msum, but generalised to Alternative.
Examples
Basic usage:
asum [Just "Hello", Nothing, Just "World"]Just "Hello"
Testing
1 declarationUtilities to test—without dabbling in IO—that your parsers do what you want them to do
Try to parse a pure environment