| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Text.LambdaOptions.Core
Description
Contains the core functionality for LambdaOptions.
Synopsis
- runOptions :: Options r () -> [String] -> Either OptionsError [r]
- data Options r a
- data OptionsError = ParseFailed {}
- type OptionCallback r f = (GetOpaqueParsers r f, Wrap r f)
- addOption :: forall r f. OptionCallback r f => Keyword -> f -> Options r ()
- getHelpDescription :: Options r () -> String
- getKeywords :: Options r () -> [Keyword]
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
an OptionsError is returned in Left.
Example program:
import System.Environment
import Text.LambdaOptions
options :: Options (IO ()) ()
options = do
addOption (kw ["--help", "-h"] `text` "Display this help text.") $ do
putStrLn "Usage:"
putStrLn $ getHelpDescription options
addOption (kw "--user" `argText` "NAME" `text` "Prints name.") $ \name -> do
putStrLn $ "Name:" ++ name
addOption (kw "--user" `argText` "NAME AGE" `text` "Prints name and age.") $ \name age -> do
putStrLn $ "Name:" ++ name ++ " Age:" ++ show (age :: Int)
main :: IO ()
main = do
args <- getArgs
case runOptions options args of
Left e -> do
putStrLn $ parseFailedMessage e
putStrLn $ getHelpDescription options
Right actions -> sequence_ actions>>>example.exe --user HaskellCurry 81 --user GraceHopperName:HaskellCurry Age:81 Name:GraceHopper>>>example.exe -hUsage: -h, --help Display this help text. --user NAME Prints name. --user NAME AGE Prints name and age.>>>example.exe --user Pythagoras LXXVUnknown option at index 2: `LXXV' Usage: -h, --help Display this help text. --user NAME Prints name. --user NAME AGE Prints name and age.
A monad for parsing options.
data OptionsError Source #
Contains information about what went wrong during an unsuccessful options parse.
Constructors
| ParseFailed | |
Fields | |
Instances
| Show OptionsError Source # | |
Defined in Text.LambdaOptions.Core Methods showsPrec :: Int -> OptionsError -> ShowS # show :: OptionsError -> String # showList :: [OptionsError] -> ShowS # | |
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 m a () 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.