Safe Haskell | None |
---|---|
Language | Haskell2010 |
Package for writing great CLI apps.
See Demo.hs for an example
This package should eventually be made its own library.
Synopsis
- type CliLog m = MonadLog Output m
- type CliThrow e m = MonadError e m
- newtype CliT e m a = CliT {}
- runCli :: MonadIO m => CliConfig -> CliT e m a -> m (Either e a)
- data CliConfig
- class Monad m => HasCliConfig m
- getCliConfig :: HasCliConfig m => m CliConfig
- data Output
- withSpinner :: (MonadIO m, MonadMask m, CliLog m, HasCliConfig m) => Text -> m a -> m a
- withSpinnerNoTrail :: (MonadIO m, MonadMask m, CliLog m, HasCliConfig m) => Text -> m a -> m a
- withSpinner' :: (MonadIO m, MonadMask m, CliLog m, HasCliConfig m) => Text -> Maybe (a -> Text) -> m a -> m a
- class AsUnstructuredError e where
- newCliConfig :: Severity -> Bool -> Bool -> IO CliConfig
- mkDefaultCliConfig :: [String] -> IO CliConfig
- getLogLevel :: (MonadIO m, HasCliConfig m) => m Severity
- putLog :: CliLog m => Severity -> Text -> m ()
- failWith :: (CliThrow e m, AsUnstructuredError e) => Text -> m a
- withExitFailMessage :: (CliLog m, MonadCatch m) => Text -> m a -> m a
- data Severity
- class AsProcessFailure e where
- data ProcessFailure = ProcessFailure CmdSpec Int
- prettyProcessFailure :: ProcessFailure -> Text
- data ProcessSpec = ProcessSpec {}
- callCommand :: (MonadIO m, CliLog m) => String -> m ()
- callProcess :: (MonadIO m, CliLog m) => String -> [String] -> m ()
- callProcessAndLogOutput :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => (Severity, Severity) -> ProcessSpec -> m ()
- createProcess_ :: (MonadIO m, CliLog m) => String -> ProcessSpec -> m (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
- overCreateProcess :: (CreateProcess -> CreateProcess) -> ProcessSpec -> ProcessSpec
- proc :: FilePath -> [String] -> ProcessSpec
- readCreateProcessWithExitCode :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e) => ProcessSpec -> m (ExitCode, String, String)
- readProcessAndLogOutput :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => (Severity, Severity) -> ProcessSpec -> m Text
- readProcessAndLogStderr :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => Severity -> ProcessSpec -> m Text
- readProcessJSONAndLogStderr :: (FromJSON a, MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => Severity -> ProcessSpec -> m a
- reconstructCommand :: CmdSpec -> Text
- setCwd :: Maybe FilePath -> ProcessSpec -> ProcessSpec
- setDelegateCtlc :: Bool -> ProcessSpec -> ProcessSpec
- setEnvOverride :: (Map String String -> Map String String) -> ProcessSpec -> ProcessSpec
- shell :: String -> ProcessSpec
- waitForProcess :: MonadIO m => ProcessHandle -> m ExitCode
Documentation
type CliThrow e m = MonadError e m Source #
Instances
Monad m => MonadError e (CliT e m) Source # | |
Defined in Cli.Extras.Types throwError :: e -> CliT e m a # catchError :: CliT e m a -> (e -> CliT e m a) -> CliT e m a # | |
Monad m => MonadLog Output (CliT e m) Source # | |
Defined in Cli.Extras.Types logMessageFree :: (forall n. Monoid n => (Output -> n) -> n) -> CliT e m () # | |
MonadTrans (CliT e) Source # | |
Defined in Cli.Extras.Types | |
Monad m => Monad (CliT e m) Source # | |
Functor m => Functor (CliT e m) Source # | |
MonadFail m => MonadFail (CliT e m) Source # | |
Defined in Cli.Extras.Types | |
Monad m => Applicative (CliT e m) Source # | |
MonadIO m => MonadIO (CliT e m) Source # | |
Defined in Cli.Extras.Types | |
MonadThrow m => MonadThrow (CliT e m) Source # | |
Defined in Cli.Extras.Types | |
MonadCatch m => MonadCatch (CliT e m) Source # | |
MonadMask m => MonadMask (CliT e m) Source # | |
Defined in Cli.Extras.Types | |
Monad m => HasCliConfig (CliT e m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: CliT e m CliConfig Source # |
class Monad m => HasCliConfig m Source #
Instances
HasCliConfig m => HasCliConfig (ExceptT e m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: ExceptT e m CliConfig Source # | |
(Monoid w, HasCliConfig m) => HasCliConfig (WriterT w m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: WriterT w m CliConfig Source # | |
HasCliConfig m => HasCliConfig (StateT s m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: StateT s m CliConfig Source # | |
HasCliConfig m => HasCliConfig (ReaderT r m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: ReaderT r m CliConfig Source # | |
Monad m => HasCliConfig (CliT e m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: CliT e m CliConfig Source # | |
HasCliConfig m => HasCliConfig (SubExceptT e eSub m) Source # | |
Defined in Cli.Extras.Types getCliConfig :: SubExceptT e eSub m CliConfig Source # |
getCliConfig :: HasCliConfig m => m CliConfig Source #
withSpinner :: (MonadIO m, MonadMask m, CliLog m, HasCliConfig m) => Text -> m a -> m a Source #
Run an action with a CLI spinner.
withSpinnerNoTrail :: (MonadIO m, MonadMask m, CliLog m, HasCliConfig m) => Text -> m a -> m a Source #
A spinner that leaves no trail after a successful run.
Use if you wish the spinner to be ephemerally visible to the user.
The 'no trail' property automatically carries over to sub-spinners (in that they won't leave a trail either).
:: (MonadIO m, MonadMask m, CliLog m, HasCliConfig m) | |
=> Text | |
-> Maybe (a -> Text) | Leave an optional trail with the given message creator |
-> m a | |
-> m a |
Advanced version that controls the display and content of the trail message.
class AsUnstructuredError e where Source #
Indicates unstructured errors form one variant (or conceptual projection) of the error type.
Shouldn't really use this, but who has time to clean up that much!
asUnstructuredError :: Prism' e Text Source #
Instances
AsUnstructuredError Text Source # | |
Defined in Cli.Extras.Logging |
getLogLevel :: (MonadIO m, HasCliConfig m) => m Severity Source #
putLog :: CliLog m => Severity -> Text -> m () Source #
Log a message to the console.
Logs safely even if there are ongoing spinners.
failWith :: (CliThrow e m, AsUnstructuredError e) => Text -> m a Source #
Like `putLog Alert` but also abrupts the program.
withExitFailMessage :: (CliLog m, MonadCatch m) => Text -> m a -> m a Source #
Intercept ExitFailure exceptions and log the given alert before exiting.
This is useful when you want to provide contextual information to a deeper failure.
Classes of severity for log messages. These have been chosen to match
syslog
severity levels
Emergency | System is unusable. By |
Alert | Should be corrected immediately. |
Critical | Critical conditions. |
Error | Error conditions. |
Warning | May indicate that an error will occur if action is not taken. |
Notice | Events that are unusual, but not error conditions. |
Informational | Normal operational messages that require no action. |
Debug | Information useful to developers for debugging the application. |
Instances
Bounded Severity | |
Enum Severity | |
Eq Severity | |
Ord Severity | |
Defined in Control.Monad.Log | |
Read Severity | |
Show Severity | |
Pretty Severity | |
Defined in Control.Monad.Log |
class AsProcessFailure e where Source #
Indicates arbitrary process failures form one variant (or conceptual projection) of the error type.
Instances
AsProcessFailure ProcessFailure Source # | |
Defined in Cli.Extras.Process |
data ProcessFailure Source #
Instances
Show ProcessFailure Source # | |
Defined in Cli.Extras.Process showsPrec :: Int -> ProcessFailure -> ShowS # show :: ProcessFailure -> String # showList :: [ProcessFailure] -> ShowS # | |
AsProcessFailure ProcessFailure Source # | |
Defined in Cli.Extras.Process |
data ProcessSpec Source #
callCommand :: (MonadIO m, CliLog m) => String -> m () Source #
Like callCommand
but also logs (debug) the command being run
callProcess :: (MonadIO m, CliLog m) => String -> [String] -> m () Source #
Like callProcess
but also logs (debug) the process being run
callProcessAndLogOutput :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => (Severity, Severity) -> ProcessSpec -> m () Source #
Like callProcess
but logs the combined output (stdout and stderr)
with the corresponding severity.
Usually this function is called as `callProcessAndLogOutput (Debug, Error)`. However some processes are known to spit out diagnostic or informative messages in stderr, in which case it is advisable to call it with a non-Error severity for stderr, like `callProcessAndLogOutput (Debug, Debug)`.
createProcess_ :: (MonadIO m, CliLog m) => String -> ProcessSpec -> m (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) Source #
Like createProcess_
but also logs (debug) the process being run
overCreateProcess :: (CreateProcess -> CreateProcess) -> ProcessSpec -> ProcessSpec Source #
readCreateProcessWithExitCode :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e) => ProcessSpec -> m (ExitCode, String, String) Source #
readProcessAndLogOutput :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => (Severity, Severity) -> ProcessSpec -> m Text Source #
Like readProcess
but logs the combined output (stdout and stderr)
with the corresponding severity.
Usually this function is called as `callProcessAndLogOutput (Debug, Error)`. However some processes are known to spit out diagnostic or informative messages in stderr, in which case it is advisable to call it with a non-Error severity for stderr, like `callProcessAndLogOutput (Debug, Debug)`.
readProcessAndLogStderr :: (MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => Severity -> ProcessSpec -> m Text Source #
readProcessJSONAndLogStderr :: (FromJSON a, MonadIO m, CliLog m, CliThrow e m, AsProcessFailure e, MonadFail m) => Severity -> ProcessSpec -> m a Source #
reconstructCommand :: CmdSpec -> Text Source #
Pretty print a CmdSpec
setCwd :: Maybe FilePath -> ProcessSpec -> ProcessSpec Source #
setDelegateCtlc :: Bool -> ProcessSpec -> ProcessSpec Source #
setEnvOverride :: (Map String String -> Map String String) -> ProcessSpec -> ProcessSpec Source #
shell :: String -> ProcessSpec Source #
waitForProcess :: MonadIO m => ProcessHandle -> m ExitCode Source #
Wrapper around waitForProcess