lambda-options-1.1.0.0: Declarative command-line parser with type-driven pattern matching.

Safe HaskellSafe
LanguageHaskell2010

Text.LambdaOptions.Core

Description

Contains the core functionality for LambdaOptions.

Synopsis

Documentation

runOptions :: Options r -> [String] -> Either OptionsError [r] Source #

Tries to parse the supplied options against input arguments. If successful, parsed option callback results are returned in Right. Otherwise OptionsError is returned in Left.

Example program:

import qualified System.Environment as Env
import qualified Text.LambdaOptions as L

options :: L.Options (IO ())
options = do

  L.addOption
    (L.kw ["--help", "-h"]
    `L.text` "Display this help text.")
    $ do
      putStrLn "Usage:"
      putStrLn $ L.getHelpDescription options

  L.addOption
    (L.kw "--add"
    `L.argText` "X Y"
    `L.text` "Adds two Doubles and prints their sum.")
    $ \x y -> do
      print $ x + (y :: Double)

main :: IO ()
main = do
  args <- Env.getArgs
  case L.runOptions options args of
    Left e -> do
      putStrLn $ L.prettyOptionsError e
      putStrLn $ L.getHelpDescription options
    Right results -> do
      sequence_ results
>>> :main --add 3 0.14
3.14
>>> :main -h
Usage:
     --add X Y               Adds two Doubles and prints their sum.
 -h, --help                  Display this help text.
>>> :main --add 0 1 --add 2 four
Bad input for `--add' at index 3: `four'
     --add X Y               Adds two Doubles and prints their sum.
 -h, --help                  Display this help text.

type Options r = OptionsM r () Source #

A context for parsing options.

data OptionsM r a Source #

A monad for parsing options.

Instances
Monad (OptionsM r) Source # 
Instance details

Defined in Text.LambdaOptions.Core

Methods

(>>=) :: OptionsM r a -> (a -> OptionsM r b) -> OptionsM r b #

(>>) :: OptionsM r a -> OptionsM r b -> OptionsM r b #

return :: a -> OptionsM r a #

fail :: String -> OptionsM r a #

Functor (OptionsM r) Source # 
Instance details

Defined in Text.LambdaOptions.Core

Methods

fmap :: (a -> b) -> OptionsM r a -> OptionsM r b #

(<$) :: a -> OptionsM r b -> OptionsM r a #

Applicative (OptionsM r) Source # 
Instance details

Defined in Text.LambdaOptions.Core

Methods

pure :: a -> OptionsM r a #

(<*>) :: OptionsM r (a -> b) -> OptionsM r a -> OptionsM r b #

liftA2 :: (a -> b -> c) -> OptionsM r a -> OptionsM r b -> OptionsM r c #

(*>) :: OptionsM r a -> OptionsM r b -> OptionsM r b #

(<*) :: OptionsM r a -> OptionsM r b -> OptionsM r a #

data OptionsError Source #

Contains information about what went wrong during an unsuccessful options parse.

type OptionCallback r f = (GetOpaqueParsers r f, Wrap r f) Source #

Describes the callback f to be called for a successfully parsed option.

The function (or value) f can have any arity and ultimately returns a value with type r

Each of the callback's arguments must have a type t which implements Parseable and Typeable.

Think of this as the following constraint synonym:

type OptionCallback r f = (f ~ ((Parseable t*, Typeable t*) => t0 -> t1 -> ... -> tN -> r))

Example callbacks:

f0 = putStrLn "Option parsed!" :: IO ()
f1 = put :: String -> State String ()
f2 = liftIO . print :: (MonadIO m) => Int -> m ()
f3 name year ratio = lift (print (name, year, ratio)) :: (MonadTrans m) => String -> Int -> Float -> m IO ()
f4 = 7 :: Int
f5 = (:) :: Double -> [Double] -> [Double]

addOption :: forall r f. OptionCallback r f => Keyword -> f -> Options r Source #

Adds the supplied option to the Options r context.

If the keyword is matched and the types of the callback's parameters can successfully be parsed, the callback is called with the parsed arguments.

getHelpDescription :: Options r -> String Source #

Produces the help description given by the input options.

getKeywords :: Options r -> [Keyword] Source #

Produces the Keywords inserted into the input options.