HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

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
valueparse :: AsUnset e => (Info Error -> Info e) -> Parser e a -> IO a
#

Parse the environment or die

Prints the help text and exits with EXIT_FAILURE on encountering a parse error.

>>> parse (header "env-parse 0.2.0") (var str "USER" (def "nobody"))
newtypenewtype Parser e a
#

An environment parser

Instances3Functor, Applicative, Alternative
newtypenewtype Mod (t :: Type -> Type) a
#

This represents a modification of the properties of a particular Parser. Combine them using the Monoid instance.

Instances2Semigroup, Monoid
  • Semigroup (Mod t a)Defined in envparse-0.6.0 · Env.Internal.Parser
  • Monoid (Mod t a)Defined in envparse-0.6.0 · Env.Internal.Parser
datadata Info e
#

Parser's metadata

valueheader :: String -> Info e -> Info e
#

Set the help text header (it usually includes the application's name and version)

valuewidthMax :: Int -> Info e -> Info e
#

Set the max info width.

Note: It will be set to 26 columns if a smaller value is passed.

datadata Var a
#

Environment variable metadata

Instances1HasHelp
  • HasHelp VarDefined in envparse-0.6.0 · Env.Internal.Parser
typetype Reader e a = String -> Either e a
#

An environment variable's value parser. Use (<=<) and (>=>) to combine these

valuesplitOn :: Char -> Reader e [String]
#

The reader that splits a string into a list of strings consuming the separator.

valuedef :: a -> Mod Var a
#

The default value of the variable

Note: specifying it means the parser won't ever fail.

valueflag
  1. :: a

    default value

  2. -> a

    active value

  3. -> String
  4. -> Mod Flag a
  5. -> Parser e a
#

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.

datadata Flag a
#

Flag metadata

Instances1HasHelp
  • HasHelp FlagDefined in envparse-0.6.0 · Env.Internal.Parser
classclass HasHelp (t :: Type -> Type) where
#

A class of things that can have a help message attached to them

Instances2HasHelp
  • HasHelp FlagDefined in envparse-0.6.0 · Env.Internal.Parser
  • HasHelp VarDefined in envparse-0.6.0 · Env.Internal.Parser
valuesensitive :: Parser e a -> Parser e a
#

Mark the enclosed variables as sensitive to remove them from the environment once they've been parsed successfully.

valuehelpDoc :: Int -> Parser e a -> String
#

A pretty-printed list of recognized environment variables suitable for usage messages

datadata Error
#

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.

Instances5Eq, Show, AsEmpty, AsUnread, AsUnset
  • Eq ErrorDefined in envparse-0.6.0 · Env.Internal.Error
  • Show ErrorDefined in envparse-0.6.0 · Env.Internal.Error
  • AsEmpty ErrorDefined in envparse-0.6.0 · Env.Internal.Error
  • AsUnread ErrorDefined in envparse-0.6.0 · Env.Internal.Error
  • AsUnset ErrorDefined in envparse-0.6.0 · Env.Internal.Error
classclass AsUnset e where
#

The class of types that contain and can be constructed from the error returned from parsing unset variables.

Methods

Instances1AsUnset
  • AsUnset ErrorDefined in envparse-0.6.0 · Env.Internal.Error
classclass AsEmpty e where
#

The class of types that contain and can be constructed from the error returned from parsing variables whose value is empty.

Methods

Instances1AsEmpty
  • AsEmpty ErrorDefined in envparse-0.6.0 · Env.Internal.Error
classclass AsUnread e where
#

The class of types that contain and can be constructed from the error returned from parsing variable whose value cannot be parsed.

Methods

Instances1AsUnread

Re-exports

5 declarations

External functions that may be useful to the consumer of the library

valueoptional :: Alternative f => f a -> f (Maybe a)
#

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:

Example1 expression
import Control.Monad.Except
Example2 expressions
canFail = throwError "it failed" :: Except String Intfinal = return 42                :: Except String Int

Can be combined by allowing the first function to fail:

Example1 expression
runExcept $ canFail *> finalLeft "it failed"
Example1 expression
runExcept $ optional canFail *> finalRight 42
value(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
#

Right-to-left composition of Kleisli arrows. (>=>), with the arguments flipped.

Note how this operator resembles function composition (.):

(.)   ::            (b ->   c) -> (a ->   b) -> a ->   c
(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
value(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
#

Left-to-right composition of Kleisli arrows.

'(bs >=> cs) a' can be understood as the do expression

do b <- bs a
   cs b

or in terms of (>>=) as

bs a >>= cs
method(<>) :: a -> a -> a
#

An associative operation.

Examples
Example1 expression
[1,2,3] <> [4,5,6][1,2,3,4,5,6]
Example1 expression
Just [1, 2, 3] <> Just [4, 5, 6]Just [1,2,3,4,5,6]
Example1 expression
putStr "Hello, " <> putStrLn "World!"Hello, World!
valueasum :: (Foldable t, Alternative f) => t (f a) -> f a
#

The sum of a collection of actions using (<|>), generalizing concat.

asum is just like msum, but generalised to Alternative.

Examples

Basic usage:

Example1 expression
asum [Just "Hello", Nothing, Just "World"]Just "Hello"

Testing

1 declaration

Utilities to test—without dabbling in IO—that your parsers do what you want them to do