packages feed

mdoc-0.6.0.1: src/Mdoc/Examples/OptEnvConf.hs

-- |
--
-- Module      : Mdoc.Examples.OptEnvConf
-- Copyright   : (c) 2026 Patrick Brisbin
-- License     : AGPL-3
-- Maintainer  : pbrisbin@gmail.com
-- Stability   : experimental
-- Portability : POSIX
module Mdoc.Examples.OptEnvConf
  ( crontab5
  , example1
  , examplerc5
  ) where

import Mdoc.Prelude

import Mdoc.Data.Named
import Mdoc.Examples.Person
import OptEnvConf hiding (name)
import OptEnvConf.Mdoc

crontab5 :: Named
crontab5 = name "crontab" "tables for driving cron" $ getPage confParser

{- FOURMOLU_DISABLE -}
confParser :: Parser ()
confParser = void $ (,)
  <$> setting [conf @String "foo", help "Foo's the fooing of fooers"]
  <*> subConfig "bar" ((,)
    <$> setting [ conf @Bool "baz", help "Bar's baz of bazzle"]
    <*> setting [ conf @[Int] "bat", help "Bar's bat is better than that"
                , example "# enable bat\nbar:\n  bat: 1"
                , example "# disable bat\nbar:\n  bat: 0"
                ])
{- FOURMOLU_ENABLE -}

example1 :: Named
example1 = name "example" "opt-env-conf example" $ getPage exampleParser

examplerc5 :: Named
examplerc5 = name "examplerc" "opt-env-conf example config" $ getPage exampleParser

{- FOURMOLU_DISABLE -}
exampleParser :: Parser ()
exampleParser = void $ (,,,,,,,)
  <$>           setting   [ conf @Person "person", help "A person"
                          , example (unlines
                            [ "person:"
                            , "  name: John"
                            ])
                          ]
  <*>           switch'   [           long "debug",                    help "Enable debug", env "DEBUG", conf "debug", example "# enable debug\ndebug: true"]
  <*>           switch'   [short 'v', long "verbose",                                                    conf "verbose"]
  <*>           option'   [short 't', long "template",                 help "A template", value "With newlines\n"]
  <*> (         switch'   [short 'a', long "apple",                    help "Use apples"]
    <|>         switch'   [short 'b', long "banana",                   help "Use bananas"])
  <*> optional (option'   [short 'i',                 metavar "INPUT",                      env "INPUT"])
  <*>           argument' [                           metavar "FILE",                                    conf "file"]
  <*>     many (argument' [                           metavar "FILE"])
{- FOURMOLU_ENABLE -}

option' :: [Builder Text] -> Parser Text
option' xs = setting $ [option, reader str] <> xs

argument' :: [Builder Text] -> Parser Text
argument' xs = setting $ [argument, reader str] <> xs

switch' :: [Builder Bool] -> Parser Bool
switch' xs = setting $ [switch True, value False] <> xs