module System.Console.CmdArgs.GetOpt(
convert, getOpt, getOpt', usageInfo,
ArgOrder(..), OptDescr(..), ArgDescr(..)
) where
import System.Console.CmdArgs.Explicit
data ArgOrder a = Permute
data OptDescr a = Option
[Char]
[String]
(ArgDescr a)
String
data ArgDescr a
= NoArg a
| ReqArg (String -> a) String
| OptArg (Maybe String -> a) String
usageInfo :: String -> [OptDescr a] -> String
usageInfo desc flags = show $ convert desc flags
getOpt :: ArgOrder a -> [OptDescr a] -> [String] -> ([a], [String], [String])
getOpt _ flags args =
case process (convert "" flags) args of
Left x -> ([],[],[x])
Right (a,b) -> (a,b,[])
getOpt' :: ArgOrder a -> [OptDescr a] -> [String] -> ([a], [String], [String], [String])
getOpt' x y z = (a,b,[],c)
where (a,b,c) = getOpt x y z
convert :: String -> [OptDescr a] -> Mode ([a],[String])
convert help flags = mode "program" ([],[]) help args (map f flags)
where
args = flagArg (\x (a,b) -> Right (a,b++[x])) "ARG"
f (Option short long x help) = case x of
NoArg x -> flagNone names (\(a,b) -> (a++[x],b)) help
ReqArg op x -> flagReq names (\x (a,b) -> Right (a++[op x],b)) x help
OptArg op x -> flagOpt "" names (\x (a,b) -> Right (a++[op $ if null x then Nothing else Just x],b)) x help
where names = map return short ++ long