{-# LANGUAGE NoImplicitPrelude #-}

module Stack.Options.HpcReportParser
  ( hpcReportOptsParser
  , pvpBoundsOption
  ) where

import qualified Data.Text as T
import           Options.Applicative
                   ( Parser, completer, completeWith, help, long, metavar
                   , option, readerError, strOption, switch
                   )
import           Options.Applicative.Builder.Extra
                   ( dirCompleter, fileExtCompleter, textArgument )
import           Options.Applicative.Types ( readerAsk )
import           Stack.Coverage ( HpcReportOpts (..) )
import           Stack.Options.Completion ( targetCompleter )
import           Stack.Prelude
import           Stack.Types.PvpBounds ( PvpBounds, parsePvpBounds )

-- | Parser for @stack hpc report@.

hpcReportOptsParser :: Parser HpcReportOpts
hpcReportOptsParser :: Parser HpcReportOpts
hpcReportOptsParser = [Text] -> Bool -> Maybe String -> Bool -> HpcReportOpts
HpcReportOpts
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Mod ArgumentFields Text -> Parser Text
textArgument
        (  forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"TARGET_OR_TIX"
        forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer (Completer
targetCompleter forall a. Semigroup a => a -> a -> a
<> [String] -> Completer
fileExtCompleter [String
".tix"])
        ))
  forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
        (  forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"all"
        forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Use results from all packages and components involved in \
                \previous --coverage run."
        )
  forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s. IsString s => Mod OptionFields s -> Parser s
strOption
        (  forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"destdir"
        forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"DIR"
        forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer Completer
dirCompleter
        forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Output directory for HTML report."
        ))
  forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
        (  forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"open"
        forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Open the report in the browser."
        )

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