papa-0.7.0: src/Papa/Base/System/Exit.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Exiting the program, with a total 'exitWith'.
module Papa.Base.System.Exit (
module P,
exitWith,
) where
import Data.Maybe (Maybe (Just, Nothing))
import System.Exit as P (
ExitCode (ExitFailure, ExitSuccess),
die,
exitFailure,
exitSuccess,
)
import qualified System.Exit as E
import Prelude (IO)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- | Total 'E.exitWith': the action that exits with the given code.
-- 'Nothing' for @'ExitFailure' 0@, which is not a failure code, and for
-- which "System.Exit"'s @exitWith@ throws an 'Prelude.IOError' instead of
-- exiting.
--
-- ==== __Examples__
--
-- >>> (() <$ exitWith (ExitFailure 1))
-- Just ()
--
-- >>> (() <$ exitWith ExitSuccess)
-- Just ()
--
-- >>> (() <$ exitWith (ExitFailure 0))
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int (Range.linearFrom 0 (-255) 255)); has _Just (exitWith (ExitFailure n) :: Maybe (IO ())) === (n /= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
exitWith :: ExitCode -> Maybe (IO a)
exitWith c =
case c of
ExitFailure 0 -> Nothing
_ -> Just (E.exitWith c)
{-# INLINE exitWith #-}