module System.Console.CmdTheLine.GetOpt where
import Data.Maybe
import Data.Traversable
import System.Console.GetOpt
import System.Console.CmdTheLine
optDescrsTerm :: [OptDescr a] -> Term [a]
optDescrsTerm = fmap catMaybes . sequenceA . map optDescrToTerm
optDescrToTerm :: OptDescr a -> Term (Maybe a)
optDescrToTerm (Option shorts longs argDescr descr) =
case argDescr of
NoArg x -> fmap (optional x) $ value $ flag $ optInf ""
ReqArg to name -> fmap (fmap to) $ value $ opt Nothing $ optInf name
OptArg to name -> fmap (Just . to) $ value $ defaultOpt Nothing Nothing $ optInf name
where
optional :: a -> Bool -> Maybe a
optional x present | present = Just x
| otherwise = Nothing
optInf :: String -> OptInfo
optInf name = (optInfo options) { optDoc = descr
, optName = name
}
options :: [String]
options = map (:[]) shorts ++ longs