Skip to content

Commit

Permalink
[#6] Implement version parsers (#18)
Browse files Browse the repository at this point in the history
Resolves #6
  • Loading branch information
chshersh authored May 21, 2022
1 parent bd74c88 commit 27b9143
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 38 deletions.
9 changes: 7 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Main (main) where
import Control.Monad.IO.Class (MonadIO (..))

import qualified Iris
import qualified Paths_iris as Autogen


newtype App a = App
Expand All @@ -11,8 +12,12 @@ newtype App a = App

appSettings :: Iris.CliEnvSettings () ()
appSettings = Iris.defaultCliEnvSettings
{ Iris.cliEnvSettingsHeaderDesc = "Iris usage example"
, Iris.cliEnvSettingsProgDesc = "A simple grep utility"
{ Iris.cliEnvSettingsHeaderDesc = "Iris usage example"
, Iris.cliEnvSettingsProgDesc = "A simple grep utility"
, Iris.cliEnvSettingsVersionSettings =
Just (Iris.defaultVersionSettings Autogen.version)
{ Iris.versionSettingsMkDesc = \v -> "Iris Example v" <> v
}
}

app :: App ()
Expand Down
4 changes: 4 additions & 0 deletions iris.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ library
executable iris-example
import: common-options
hs-source-dirs: app

main-is: Main.hs
autogen-modules: Paths_iris
other-modules: Paths_iris

build-depends: iris
ghc-options: -threaded
-rtsopts
Expand Down
130 changes: 94 additions & 36 deletions src/Iris/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,99 @@ Environment of a CLI app.


module Iris.Env
( -- * CLI application environment
CliEnv (..)

-- * Settings for the CLI app
, CliEnvSettings (..)
( -- * Settings for the CLI app
-- ** Global CLI settings
CliEnvSettings (..)
, defaultCliEnvSettings
-- ** Application version settings
, VersionSettings (..)
, defaultVersionSettings

-- * CLI application environment
, CliEnv (..)
, mkCliEnv
) where

import Data.Kind (Type)
import Data.Version (Version, showVersion)

import qualified Options.Applicative as Opt


{- | CLI application environment. It contains default settings for
every CLI app and parameter
Has the following type parameters:
* @cmd@ — application commands
* @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
{ -- | @since 0.0.0.0
cliEnvCmd :: cmd
data CliEnvSettings (cmd :: Type) (appEnv :: Type) = CliEnvSettings
{ -- | @since 0.0.0.0
cliEnvSettingsCmdParser :: Opt.Parser cmd

-- | @since 0.0.0.0
, cliEnvAppEnv :: appEnv
, cliEnvSettingsAppEnv :: appEnv

-- | @since 0.0.0.0
, cliEnvSettingsHeaderDesc :: String

-- | @since 0.0.0.0
, cliEnvSettingsProgDesc :: String

-- | @since 0.0.0.0
, cliEnvSettingsVersionSettings :: Maybe VersionSettings
}


{- |
@since 0.0.0.0
-}
data CliEnvSettings (cmd :: Type) (appEnv :: Type) = CliEnvSettings
{ -- | @since 0.0.0.0
cliEnvSettingsCmdParser :: Opt.Parser cmd
defaultCliEnvSettings :: CliEnvSettings () ()
defaultCliEnvSettings = CliEnvSettings
{ cliEnvSettingsCmdParser = pure ()
, cliEnvSettingsAppEnv = ()
, cliEnvSettingsHeaderDesc = "Simple CLI program"
, cliEnvSettingsProgDesc = "CLI tool build with iris - a Haskell CLI framework"
, cliEnvSettingsVersionSettings = Nothing
}

-- | @since 0.0.0.0
, cliEnvSettingsAppEnv :: appEnv
{- |
-- | @since 0.0.0.0
, cliEnvSettingsHeaderDesc :: String
@since 0.0.0.0
-}
data VersionSettings = VersionSettings
{ -- | @since 0.0.0.0
versionSettingsVersion :: Version

-- | @since 0.0.0.0
, cliEnvSettingsProgDesc :: String
, versionSettingsMkDesc :: String -> 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"
defaultVersionSettings :: Version -> VersionSettings
defaultVersionSettings version = VersionSettings
{ versionSettingsVersion = version
, versionSettingsMkDesc = id
}

{- | CLI application environment. It contains default settings for
every CLI app and parameter
Has the following type parameters:
* @cmd@ — application commands
* @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
{ -- | @since 0.0.0.0
cliEnvCmd :: cmd

-- | @since 0.0.0.0
, cliEnvAppEnv :: appEnv
}

{- |
Expand All @@ -92,8 +124,34 @@ mkCliEnv CliEnvSettings{..} = do
}
where
cmdParserInfo :: Opt.ParserInfo cmd
cmdParserInfo = Opt.info (Opt.helper <*> cliEnvSettingsCmdParser) $ mconcat
[ Opt.fullDesc
, Opt.header cliEnvSettingsHeaderDesc
, Opt.progDesc cliEnvSettingsProgDesc
]
cmdParserInfo = Opt.info
( Opt.helper
<*> mkVersionParser cliEnvSettingsVersionSettings
<*> cliEnvSettingsCmdParser
)
$ mconcat
[ Opt.fullDesc
, Opt.header cliEnvSettingsHeaderDesc
, Opt.progDesc cliEnvSettingsProgDesc
]

mkVersionParser :: Maybe VersionSettings -> Opt.Parser (a -> a)
mkVersionParser = maybe (pure id) fullVersionP

fullVersionP :: VersionSettings -> Opt.Parser (a -> a)
fullVersionP VersionSettings{..} = versionP <*> numericVersionP
where
versionStr :: String
versionStr = showVersion versionSettingsVersion

versionP :: Opt.Parser (a -> a)
versionP = Opt.infoOption (versionSettingsMkDesc versionStr) $ mconcat
[ Opt.long "version"
, Opt.help "Show application version"
]

numericVersionP :: Opt.Parser (a -> a)
numericVersionP = Opt.infoOption versionStr $ mconcat
[ Opt.long "numeric-version"
, Opt.help "Show only numeric application version"
]

0 comments on commit 27b9143

Please sign in to comment.