simple-get-opt 0.3 → 0.4
raw patch · 2 files changed
+22/−11 lines, 2 files
Files
- simple-get-opt.cabal +1/−1
- src/SimpleGetOpt.hs +21/−10
simple-get-opt.cabal view
@@ -1,5 +1,5 @@ name: simple-get-opt-version: 0.3+version: 0.4 synopsis: A simple library for processing command-line options. description: A simple library for processing command-line options.
src/SimpleGetOpt.hs view
@@ -49,6 +49,7 @@ ( -- * Basic functionality getOpts , getOptsX+ , getOptsFrom , OptSpec(..) , OptDescr(..) , OptSetter@@ -110,11 +111,11 @@ -- ^ This option does not take an argument. | ReqArg String (String -> OptSetter a)- -- ^ This optoin has a required arugment.+ -- ^ This option has a required argument. -- The string describes the type of the argument. | OptArg String (Maybe String -> OptSetter a)- -- ^ This optoin has an optional arugment.+ -- ^ This option has an optional argument. -- The string describes the type of the argument. @@ -144,20 +145,30 @@ Right a1 -> (a1,es) +-- | Process the given command line options according to the given spec.+-- The options will be permuted to get flags.+-- Returns errors on the 'Left'.+getOptsFrom :: OptSpec a -> [String] -> Either GetOptException a+getOptsFrom os as =+ do let (funs,files,errs) = GetOpt.getOpt GetOpt.Permute (specToGetOpt os) as+ unless (null errs) $ Left (GetOptException errs)+ let (a, errs1) = foldl addOpt (progDefaults os,[]) funs+ unless (null errs1) $ Left (GetOptException errs1)+ let (b, errs2) = foldl (addFile (progParams os)) (a,[]) files+ unless (null errs2) $ Left (GetOptException errs2)+ pure b+++ -- | Get the command-line options and process them according to the given spec. -- The options will be permuted to get flags. -- Throws a 'GetOptException' if some problems are found. getOptsX :: OptSpec a -> IO a getOptsX os = do as <- getArgs- let (funs,files,errs) = GetOpt.getOpt GetOpt.Permute (specToGetOpt os) as- unless (null errs) $ throwIO (GetOptException errs)- let (a, errs1) = foldl addOpt (progDefaults os,[]) funs- unless (null errs1) $ throwIO (GetOptException errs1)- let (b, errs2) = foldl (addFile (progParams os)) (a,[]) files- unless (null errs2) $ throwIO (GetOptException errs2)- return b-+ case getOptsFrom os as of+ Left e -> throwIO e+ Right a -> pure a -- | Get the command-line options and process them according to the given spec. -- The options will be permuted to get flags.