module System.Console.Action.Internal
(
Action
, run
, simple
, withArgument
, usingConfiguration
) where
import qualified System.Console.Argument as Argument
import System.Exit (exitFailure)
data Action c
= Action
{
run :: [String] -> c -> IO ()
}
simple :: IO () -> Action c
simple = Action . const . const
withArgument :: Argument.Type x -> (x -> Action c) -> Action c
withArgument at f = Action g where
g (x : xs) = either
(const . (>> exitFailure) . putStrLn)
(\ y -> run (f y) xs)
(Argument.parser at x)
g [] = const $ putStrLn ("Error: missing argument of type " ++ Argument.name at) >> exitFailure
usingConfiguration :: (c -> Action c) -> Action c
usingConfiguration f = Action (\ nonOptions settings -> run (f settings) nonOptions settings)