[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:57:49 UTC 2010
The following commit has been merged in the master branch:
commit 20d4117965e9a268357292f26ebbb6f7a5084a1f
Author: John Goerzen <jgoerzen at complete.org>
Date: Mon Apr 18 23:01:35 2005 +0100
Added docs, validateCmdLine
diff --git a/MissingH/GetOpt.hs b/MissingH/GetOpt.hs
index 54e31ed..c5d2576 100644
--- a/MissingH/GetOpt.hs
+++ b/MissingH/GetOpt.hs
@@ -31,12 +31,21 @@ Written by John Goerzen, jgoerzen\@complete.org
Utilities for command-line parsing, including wrappers around
the standard System.Console.GetOpt module.
-}
-module MissingH.GetOpt (
+module MissingH.GetOpt (parseCmdLine,
+ validateCmdLine
)
where
import System.Console.GetOpt
import System.Environment
+{- | Simple command line parser -- a basic wrapper around the system's
+default getOpt. See the System.Console.GetOpt manual for a description of the
+first two parameters.
+
+The third parameter is a usage information header.
+
+The return value consists of the list of parsed flags and a list of
+non-option arguments. -}
parseCmdLine :: ArgOrder a -> [OptDescr a] -> String -> IO ([a], [String])
parseCmdLine order options header =
do argv <- getArgs
@@ -44,3 +53,21 @@ parseCmdLine order options header =
(o, n, []) -> return (o, n)
(_, _, errors) -> ioError (userError (concat errors ++
usageInfo header options))
+
+{- | Similar to 'parseCmdLine', but takes an additional function that validates
+the post-parse command-line arguments. This is useful, for example, in
+situations where there are two arguments that are mutually-exclusive and only
+one may legitimately be given at a time.
+
+The return value of the function indicates whether or not it detected an
+error condition. If it returns Nothing, there is no error. If it returns
+Just String, there was an error, described by the String.
+-}
+validateCmdLine :: ArgOrder a -> [OptDescr a] -> String ->
+ (([a],[String]) -> Maybe String) -> IO ([a], [String])
+validateCmdLine order options header func =
+ do res <- parseCmdLine order options header
+ case func res of
+ Nothing -> return res
+ Just error -> ioError (userError (concat [error] ++
+ usageInfo header options))
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list