Add proper cmd-line arg handling
This commit is contained in:
parent
02a9e81251
commit
da0630dfb1
12
README.md
12
README.md
@ -6,6 +6,18 @@ Tempered
|
|||||||
A dead-simple templating utility for simple shell interpolation.
|
A dead-simple templating utility for simple shell interpolation.
|
||||||
Use at your own risk and only on trusted templates.
|
Use at your own risk and only on trusted templates.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ tempered --help
|
||||||
|
Tempered - Templating engine based on shell interpolation
|
||||||
|
|
||||||
|
Usage: tempered [templates]
|
||||||
|
Interpolate templates to stdout
|
||||||
|
|
||||||
|
Available options:
|
||||||
|
templates The templates to interpolate
|
||||||
|
-h,--help Show this help text
|
||||||
|
```
|
||||||
|
|
||||||
Here's a simple use-case:
|
Here's a simple use-case:
|
||||||
|
|
||||||
```md
|
```md
|
||||||
|
35
app/Main.hs
35
app/Main.hs
@ -1,24 +1,55 @@
|
|||||||
|
{-# language OverloadedStrings #-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import System.Environment
|
import System.Environment
|
||||||
import System.Directory
|
import System.Directory
|
||||||
|
import System.Exit
|
||||||
|
|
||||||
|
import Paths_tempered (version)
|
||||||
|
import Data.Version (showVersion)
|
||||||
|
|
||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
|
import Data.Monoid
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Control.Monad.Reader
|
import Control.Monad.Reader
|
||||||
|
|
||||||
|
import Options.Applicative
|
||||||
|
|
||||||
import Tempered.Options
|
import Tempered.Options
|
||||||
import Tempered.Parser
|
import Tempered.Parser
|
||||||
import Tempered.Template
|
import Tempered.Template
|
||||||
|
|
||||||
|
data Options = Options
|
||||||
|
{ files :: [String]
|
||||||
|
, versionFlag :: Bool
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
|
||||||
|
options :: Parser Options
|
||||||
|
options = Options <$> many (argument str meta) <*> switch vers
|
||||||
|
where meta = help "The templates to interpolate" <> metavar "templates"
|
||||||
|
vers = long "version" <> short 'v' <> help "Show version"
|
||||||
|
|
||||||
|
argParser :: ParserInfo Options
|
||||||
|
argParser = info (options <**> helper)
|
||||||
|
( fullDesc <>
|
||||||
|
progDesc "Interpolate templates to stdout" <>
|
||||||
|
header "Tempered - Templating engine based on shell interpolation")
|
||||||
|
|
||||||
-- | Run tempered on cmdline args.
|
-- | Run tempered on cmdline args.
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
Options filenames versionF <- execParser argParser
|
||||||
|
when versionF $ putStrLn (showVersion version) >> exitSuccess
|
||||||
envVars <- getEnvVars
|
envVars <- getEnvVars
|
||||||
filenames <- getArgs
|
templates <- getTemplates filenames
|
||||||
templates <- traverse (templateFromFile >=> handleTemplateError) filenames
|
|
||||||
runReaderT (renderOutput templates) envVars
|
runReaderT (renderOutput templates) envVars
|
||||||
where
|
where
|
||||||
|
-- Choose stdin if we're not given any files
|
||||||
|
getTemplates [] = (:[]) <$> readStdInTemplate
|
||||||
|
getTemplates filenames = traverse (templateFromFile >=> handleTemplateError) filenames
|
||||||
|
|
||||||
|
readStdInTemplate = getContents >>= handleTemplateError . parseTemplate "stdin"
|
||||||
renderOutput = traverse_ (interpTemplate >=> liftIO . putStr)
|
renderOutput = traverse_ (interpTemplate >=> liftIO . putStr)
|
||||||
|
|
||||||
-- | Combine local and global environment variables
|
-- | Combine local and global environment variables
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: tempered
|
name: tempered
|
||||||
version: 0.1.0
|
version: 0.2.0
|
||||||
synopsis: A dead-simple shell interpolation templating utility
|
synopsis: A dead-simple shell interpolation templating utility
|
||||||
-- description: A dead-simple shell interpolation templating utility
|
-- description: A dead-simple shell interpolation templating utility
|
||||||
homepage: https://github.com/ChrisPenner/tempered#readme
|
homepage: https://github.com/ChrisPenner/tempered#readme
|
||||||
@ -37,6 +37,9 @@ executable tempered
|
|||||||
, mtl
|
, mtl
|
||||||
, tempered
|
, tempered
|
||||||
, containers
|
, containers
|
||||||
|
, optparse-applicative
|
||||||
|
-- Required for --version flag
|
||||||
|
other-modules: Paths_tempered
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
test-suite tempered-test
|
test-suite tempered-test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user