{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}

{-|
Module      : Stack.Options.PvpBoundsParser
Description : Parser for PVP bounds.
License     : BSD-3-Clause

Parser for PVP bounds.
-}

module Stack.Options.PvpBoundsParser
  ( pvpBoundsParser
  ) where

import qualified Data.Text as T
import           Options.Applicative
                   ( Parser, completeWith, help, long, metavar, option
                   , readerError
                   )
import           Options.Applicative.Types ( readerAsk )
import           Stack.Prelude
import           Stack.Types.PvpBounds ( PvpBounds (..), parsePvpBounds )

-- | Parser for PVP bounds.

pvpBoundsParser ::
     Maybe Text
     -- ^ Optional context for the option's help message.

  -> Parser PvpBounds
pvpBoundsParser :: Maybe Text -> Parser PvpBounds
pvpBoundsParser Maybe Text
context = ReadM PvpBounds -> Mod OptionFields PvpBounds -> Parser PvpBounds
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM PvpBounds
readPvpBounds
  (  String -> Mod OptionFields PvpBounds
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"pvp-bounds"
  Mod OptionFields PvpBounds
-> Mod OptionFields PvpBounds -> Mod OptionFields PvpBounds
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields PvpBounds
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"PVP-BOUNDS"
  Mod OptionFields PvpBounds
-> Mod OptionFields PvpBounds -> Mod OptionFields PvpBounds
forall a. Semigroup a => a -> a -> a
<> [String] -> Mod OptionFields PvpBounds
forall (f :: * -> *) a. HasCompleter f => [String] -> Mod f a
completeWith [String
"none", String
"lower", String
"upper", String
"both"]
  Mod OptionFields PvpBounds
-> Mod OptionFields PvpBounds -> Mod OptionFields PvpBounds
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields PvpBounds
forall (f :: * -> *) a. String -> Mod f a
help (Text -> String
T.unpack Text
helpMsg)
  )
 where
  readPvpBounds :: ReadM PvpBounds
readPvpBounds = do
    String
s <- ReadM String
readerAsk
    case Text -> Either String PvpBounds
parsePvpBounds (Text -> Either String PvpBounds)
-> Text -> Either String PvpBounds
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
s of
      Left String
e -> String -> ReadM PvpBounds
forall a. String -> ReadM a
readerError String
e
      Right PvpBounds
v -> PvpBounds -> ReadM PvpBounds
forall a. a -> ReadM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PvpBounds
v
  helpMsg :: Text
helpMsg =
       Text
helpMsgPrefix
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" PVP version bounds should be added to Cabal file: none, lower, upper, \
       \both."
  helpMsgPrefix :: Text
helpMsgPrefix = Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"How" (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", how") Maybe Text
context