{-# LANGUAGE DeriveFunctor #-}
module Options.Harg.Types where
import System.Environment (getArgs, getEnvironment)
type OptReader a = String -> Either String a
data Opt a
= Opt
{ _optLong :: Maybe String
, _optShort :: Maybe Char
, _optHelp :: Maybe String
, _optMetavar :: Maybe String
, _optEnvVar :: Maybe String
, _optDefault :: Maybe a
, _optReader :: OptReader a
, _optType :: OptType a
}
deriving Functor
data OptType a
= OptionOptType
| FlagOptType a
| ArgumentOptType
deriving Functor
data OptAttr
= OptDefault
| OptOptional
data OptionOpt (attr :: [OptAttr]) a
= OptionOpt
{ _oLong :: Maybe String
, _oShort :: Maybe Char
, _oHelp :: Maybe String
, _oMetavar :: Maybe String
, _oEnvVar :: Maybe String
, _oDefault :: Maybe a
, _oReader :: OptReader a
}
data FlagOpt (attr :: [OptAttr]) a
= FlagOpt
{ _fLong :: Maybe String
, _fShort :: Maybe Char
, _fHelp :: Maybe String
, _fEnvVar :: Maybe String
, _fDefault :: a
, _fReader :: OptReader a
, _fActive :: a
}
data ArgumentOpt (attr :: [OptAttr]) a
= ArgumentOpt
{ _aHelp :: Maybe String
, _aMetavar :: Maybe String
, _aEnvVar :: Maybe String
, _aDefault :: Maybe a
, _aReader :: OptReader a
}
data OptError
= OptError
{ _oeOpt :: SomeOpt
, _oeSource :: Maybe String
, _oeDesc :: String
}
data SomeOpt where
SomeOpt :: Opt a -> SomeOpt
type Environment
= [(String, String)]
type Args
= [String]
data HargCtx
= HargCtx
{ _hcEnv :: Environment
, _hcArgs :: Args
}
getCtx :: IO HargCtx
getCtx
= HargCtx <$> getEnvironment <*> getArgs
ctxFromArgs :: Args -> IO HargCtx
ctxFromArgs args
= HargCtx <$> getEnvironment <*> pure args
ctxFromEnv :: Environment -> IO HargCtx
ctxFromEnv env
= HargCtx <$> pure env <*> getArgs
pureCtx :: Environment -> Args -> HargCtx
pureCtx
= HargCtx
toOptError
:: Opt a
-> Maybe String
-> String
-> OptError
toOptError
= OptError . SomeOpt