Skip to content

Commit

Permalink
[#7] Add --help parsers (#15)
Browse files Browse the repository at this point in the history
Resolves #7
  • Loading branch information
chshersh authored May 21, 2022
1 parent 2231941 commit bd74c88
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 14 deletions.
22 changes: 22 additions & 0 deletions app/Main.hs
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
12 changes: 11 additions & 1 deletion iris.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ common common-options
RecordWildCards
ScopedTypeVariables
StandaloneDeriving
StrictData
TupleSections
TypeApplications
ViewPatterns
Expand All @@ -69,9 +70,18 @@ library
Iris.Env

build-depends:
, mtl >= 2.2 && < 2.4
, mtl >= 2.2 && < 2.4
, optparse-applicative ^>= 0.17

executable iris-example
import: common-options
hs-source-dirs: app
main-is: Main.hs
build-depends: iris
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N

test-suite iris-test
import: common-options
type: exitcode-stdio-1.0
Expand Down
2 changes: 1 addition & 1 deletion src/Iris.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 31 additions & 7 deletions src/Iris/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
71 changes: 66 additions & 5 deletions src/Iris/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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
]

0 comments on commit bd74c88

Please sign in to comment.