console-program-0.2.0.0: src/System/Console/Action/Internal.hs
module System.Console.Action.Internal
(
Action
, run
, simple
, withArgument
, usingConfiguration
) where
import qualified System.Console.Argument as Argument
import System.Exit (exitFailure)
-- | An @Action s@ is an @IO@ action, which may take arguments
-- (\"non-options\") from the command line, and which may use settings
-- of type @s@.
data Action s
= Action
{
run :: [String] -> s -> IO ()
}
-- | A simple action, taking no argument.
simple :: IO () -> Action s
simple = Action . const . const
-- | Create an action that takes an argument (non-option).
--
-- The type of argument is specified by the first parameter; such values can
-- be obtained from the module "System.Console.Argument".
withArgument :: Argument.Type x -> (x -> Action s) -> Action s
withArgument at f = Action g where
g (x : xs) = either
(const . (>> exitFailure) . putStrLn) -- Show errors and exit.
(\ y -> run (f y) xs) -- Argument parsing succeeded; run the action.
(Argument.parser at x)
g [] = const $ putStrLn ("Error: missing argument of type " ++ Argument.name at) >> exitFailure
-- | Create an action that depends on the program configuration (determined
-- by the configuration file and command line arguments).
usingConfiguration :: (s -> Action s) -> Action s
usingConfiguration f = Action (\ nonOptions settings -> run (f settings) nonOptions settings)