-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #7
- Loading branch information
Showing
5 changed files
with
131 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Main (main) where | ||
|
||
import Control.Monad.IO.Class (MonadIO (..)) | ||
|
||
import qualified Iris | ||
|
||
|
||
newtype App a = App | ||
{ unApp :: Iris.CliApp () () a | ||
} deriving newtype (Functor, Applicative, Monad, MonadIO) | ||
|
||
appSettings :: Iris.CliEnvSettings () () | ||
appSettings = Iris.defaultCliEnvSettings | ||
{ Iris.cliEnvSettingsHeaderDesc = "Iris usage example" | ||
, Iris.cliEnvSettingsProgDesc = "A simple grep utility" | ||
} | ||
|
||
app :: App () | ||
app = liftIO $ putStrLn "Hello from an Iris app!" | ||
|
||
main :: IO () | ||
main = Iris.runCliApp appSettings $ unApp app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ Module : Iris | |
Copyright : (c) 2022 Dmitrii Kovanikov | ||
SPDX-License-Identifier : MPL-2.0 | ||
Maintainer : Dmitrii Kovanikov <[email protected]> | ||
Stability : Stable | ||
Stability : Experimental | ||
Portability : Portable | ||
Haskell CLI framework | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ Module : Iris.Env | |
Copyright : (c) 2022 Dmitrii Kovanikov | ||
SPDX-License-Identifier : MPL-2.0 | ||
Maintainer : Dmitrii Kovanikov <[email protected]> | ||
Stability : Stable | ||
Stability : Experimental | ||
Portability : Portable | ||
Haskell CLI framework | ||
|
@@ -14,24 +14,48 @@ Haskell CLI framework | |
-} | ||
|
||
module Iris.App | ||
( App (..) | ||
( CliApp (..) | ||
, runCliApp | ||
, runCliAppManually | ||
) where | ||
|
||
import Control.Monad.IO.Class (MonadIO (..)) | ||
import Control.Monad.Reader (ReaderT (..)) | ||
import Control.Monad.IO.Class (MonadIO) | ||
import Control.Monad.Reader (MonadReader, ReaderT (..)) | ||
|
||
import Iris.Env (CliEnv (..)) | ||
import Iris.Env (CliEnv, CliEnvSettings, mkCliEnv) | ||
|
||
|
||
{- | Main monad for your CLI application. | ||
@since 0.0.0.0 | ||
-} | ||
newtype App cmd appEnv a = App | ||
{ unApp :: CliEnv cmd appEnv -> IO a | ||
newtype CliApp cmd appEnv a = CliApp | ||
{ unCliApp :: CliEnv cmd appEnv -> IO a | ||
} deriving | ||
( Functor | ||
-- ^ @since 0.0.0.0 | ||
, Applicative | ||
-- ^ @since 0.0.0.0 | ||
, Monad | ||
-- ^ @since 0.0.0.0 | ||
, MonadIO | ||
-- ^ @since 0.0.0.0 | ||
, MonadReader (CliEnv cmd appEnv) | ||
-- ^ @since 0.0.0.0 | ||
) via ReaderT (CliEnv cmd appEnv) IO | ||
|
||
{- | Run application with settings. | ||
@since 0.0.0.0 | ||
-} | ||
runCliApp :: CliEnvSettings cmd appEnv -> CliApp cmd appEnv a -> IO a | ||
runCliApp settings cliApp = do | ||
cliEnv <- mkCliEnv settings | ||
runCliAppManually cliEnv cliApp | ||
|
||
{- | Run application by constructing 'CliEnv' settings manually. | ||
@since 0.0.0.0 | ||
-} | ||
runCliAppManually :: CliEnv cmd appEnv -> CliApp cmd appEnv a -> IO a | ||
runCliAppManually cliEnv (CliApp run) = run cliEnv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ Module : Iris.Env | |
Copyright : (c) 2022 Dmitrii Kovanikov | ||
SPDX-License-Identifier : MPL-2.0 | ||
Maintainer : Dmitrii Kovanikov <[email protected]> | ||
Stability : Stable | ||
Stability : Experimental | ||
Portability : Portable | ||
Environment of a CLI app. | ||
|
@@ -13,7 +13,13 @@ Environment of a CLI app. | |
|
||
|
||
module Iris.Env | ||
( CliEnv (..) | ||
( -- * CLI application environment | ||
CliEnv (..) | ||
|
||
-- * Settings for the CLI app | ||
, CliEnvSettings (..) | ||
, defaultCliEnvSettings | ||
, mkCliEnv | ||
) where | ||
|
||
import Data.Kind (Type) | ||
|
@@ -27,12 +33,67 @@ every CLI app and parameter | |
Has the following type parameters: | ||
* @cmd@ — application commands | ||
* @appEnv@ — application specific environment; use @()@ if you don't | ||
* @appEnv@ — application-specific environment; use @()@ if you don't | ||
have custom app environment | ||
@since 0.0.0.0 | ||
-} | ||
data CliEnv (cmd :: Type) (appEnv :: Type) = CliEnv | ||
{ cliEnvCmdParser :: Opt.Parser cmd | ||
, cliEnvAppEnv :: appEnv | ||
{ -- | @since 0.0.0.0 | ||
cliEnvCmd :: cmd | ||
|
||
-- | @since 0.0.0.0 | ||
, cliEnvAppEnv :: appEnv | ||
} | ||
|
||
{- | | ||
@since 0.0.0.0 | ||
-} | ||
data CliEnvSettings (cmd :: Type) (appEnv :: Type) = CliEnvSettings | ||
{ -- | @since 0.0.0.0 | ||
cliEnvSettingsCmdParser :: Opt.Parser cmd | ||
|
||
-- | @since 0.0.0.0 | ||
, cliEnvSettingsAppEnv :: appEnv | ||
|
||
-- | @since 0.0.0.0 | ||
, cliEnvSettingsHeaderDesc :: String | ||
|
||
-- | @since 0.0.0.0 | ||
, cliEnvSettingsProgDesc :: String | ||
} | ||
|
||
{- | | ||
@since 0.0.0.0 | ||
-} | ||
defaultCliEnvSettings :: CliEnvSettings () () | ||
defaultCliEnvSettings = CliEnvSettings | ||
{ cliEnvSettingsCmdParser = pure () | ||
, cliEnvSettingsAppEnv = () | ||
, cliEnvSettingsHeaderDesc = "Simple CLI program" | ||
, cliEnvSettingsProgDesc = "CLI tool build with iris - a Haskell CLI framework" | ||
} | ||
|
||
{- | | ||
@since 0.0.0.0 | ||
-} | ||
mkCliEnv | ||
:: forall cmd appEnv | ||
. CliEnvSettings cmd appEnv | ||
-> IO (CliEnv cmd appEnv) | ||
mkCliEnv CliEnvSettings{..} = do | ||
cmd <- Opt.execParser cmdParserInfo | ||
pure CliEnv | ||
{ cliEnvCmd = cmd | ||
, cliEnvAppEnv = cliEnvSettingsAppEnv | ||
} | ||
where | ||
cmdParserInfo :: Opt.ParserInfo cmd | ||
cmdParserInfo = Opt.info (Opt.helper <*> cliEnvSettingsCmdParser) $ mconcat | ||
[ Opt.fullDesc | ||
, Opt.header cliEnvSettingsHeaderDesc | ||
, Opt.progDesc cliEnvSettingsProgDesc | ||
] |