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

module Stack.Options.GhciParser
  ( ghciOptsParser
  ) where

import           Options.Applicative
                   ( Parser, completer, flag, help, idm, internal, long, metavar
                   , strOption, switch
                   )
import           Options.Applicative.Args ( argsOption )
import           Options.Applicative.Builder.Extra
                   ( boolFlags, boolFlagsNoDefault, fileExtCompleter
                   , textArgument, textOption
                   )
import           Stack.Config ( packagesParser )
import           Stack.Ghci ( GhciOpts (..) )
import           Stack.Options.BuildParser ( flagsParser )
import           Stack.Options.Completion
                   ( ghcOptsCompleter, targetCompleter )
import           Stack.Prelude

-- | Parser for GHCI options

ghciOptsParser :: Parser GhciOpts
ghciOptsParser :: Parser GhciOpts
ghciOptsParser = [Text]
-> [String]
-> [String]
-> Map ApplyCLIFlag (Map FlagName Bool)
-> Maybe String
-> Bool
-> [String]
-> Maybe Text
-> Bool
-> Bool
-> Maybe Bool
-> Bool
-> Bool
-> GhciOpts
GhciOpts
  ([Text]
 -> [String]
 -> [String]
 -> Map ApplyCLIFlag (Map FlagName Bool)
 -> Maybe String
 -> Bool
 -> [String]
 -> Maybe Text
 -> Bool
 -> Bool
 -> Maybe Bool
 -> Bool
 -> Bool
 -> GhciOpts)
-> Parser [Text]
-> Parser
     ([String]
      -> [String]
      -> Map ApplyCLIFlag (Map FlagName Bool)
      -> Maybe String
      -> Bool
      -> [String]
      -> Maybe Text
      -> Bool
      -> Bool
      -> Maybe Bool
      -> Bool
      -> Bool
      -> GhciOpts)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text -> Parser [Text]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Mod ArgumentFields Text -> Parser Text
textArgument
        (  String -> Mod ArgumentFields Text
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"TARGET/FILE"
        Mod ArgumentFields Text
-> Mod ArgumentFields Text -> Mod ArgumentFields Text
forall a. Semigroup a => a -> a -> a
<> Completer -> Mod ArgumentFields Text
forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer (Completer
targetCompleter Completer -> Completer -> Completer
forall a. Semigroup a => a -> a -> a
<> [String] -> Completer
fileExtCompleter [String
".hs", String
".lhs"])
        Mod ArgumentFields Text
-> Mod ArgumentFields Text -> Mod ArgumentFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod ArgumentFields Text
forall (f :: * -> *) a. String -> Mod f a
help String
"If none specified, use all local packages. See \
                \https://docs.haskellstack.org/en/stable/build_command/#target-syntax \
                \for details. If a path to a .hs or .lhs file is specified, it \
                \will be loaded."
        ))
  Parser
  ([String]
   -> [String]
   -> Map ApplyCLIFlag (Map FlagName Bool)
   -> Maybe String
   -> Bool
   -> [String]
   -> Maybe Text
   -> Bool
   -> Bool
   -> Maybe Bool
   -> Bool
   -> Bool
   -> GhciOpts)
-> Parser [String]
-> Parser
     ([String]
      -> Map ApplyCLIFlag (Map FlagName Bool)
      -> Maybe String
      -> Bool
      -> [String]
      -> Maybe Text
      -> Bool
      -> Bool
      -> Maybe Bool
      -> Bool
      -> Bool
      -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (     (\[String]
x [[String]]
y -> [String]
x [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[String]]
y)
        ([String] -> [[String]] -> [String])
-> Parser [String] -> Parser ([[String]] -> [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [String] -> [String] -> Mod FlagFields [String] -> Parser [String]
forall a. a -> a -> Mod FlagFields a -> Parser a
flag
              []
              [String
"-Wall", String
"-Werror"]
              (  String -> Mod FlagFields [String]
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"pedantic"
              Mod FlagFields [String]
-> Mod FlagFields [String] -> Mod FlagFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields [String]
forall (f :: * -> *) a. String -> Mod f a
help String
"Turn on -Wall and -Werror."
              )
        Parser ([[String]] -> [String])
-> Parser [[String]] -> Parser [String]
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [String] -> Parser [[String]]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Mod OptionFields [String] -> Parser [String]
argsOption
              (  String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"ghci-options"
              Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"OPTIONS"
              Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> Completer -> Mod OptionFields [String]
forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer Completer
ghcOptsCompleter
              Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. String -> Mod f a
help String
"Additional options passed to GHCi (can be specified \
                      \multiple times)."
              ))
      )
  Parser
  ([String]
   -> Map ApplyCLIFlag (Map FlagName Bool)
   -> Maybe String
   -> Bool
   -> [String]
   -> Maybe Text
   -> Bool
   -> Bool
   -> Maybe Bool
   -> Bool
   -> Bool
   -> GhciOpts)
-> Parser [String]
-> Parser
     (Map ApplyCLIFlag (Map FlagName Bool)
      -> Maybe String
      -> Bool
      -> [String]
      -> Maybe Text
      -> Bool
      -> Bool
      -> Maybe Bool
      -> Bool
      -> Bool
      -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (     [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
        ([[String]] -> [String]) -> Parser [[String]] -> Parser [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [String] -> Parser [[String]]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Mod OptionFields [String] -> Parser [String]
argsOption
              (  String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"ghc-options"
              Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"OPTIONS"
              Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> Completer -> Mod OptionFields [String]
forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer Completer
ghcOptsCompleter
              Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. String -> Mod f a
help String
"Additional options passed to both GHC and GHCi (can be \
                      \specified multiple times)."
              ))
      )
  Parser
  (Map ApplyCLIFlag (Map FlagName Bool)
   -> Maybe String
   -> Bool
   -> [String]
   -> Maybe Text
   -> Bool
   -> Bool
   -> Maybe Bool
   -> Bool
   -> Bool
   -> GhciOpts)
-> Parser (Map ApplyCLIFlag (Map FlagName Bool))
-> Parser
     (Maybe String
      -> Bool
      -> [String]
      -> Maybe Text
      -> Bool
      -> Bool
      -> Maybe Bool
      -> Bool
      -> Bool
      -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Map ApplyCLIFlag (Map FlagName Bool))
flagsParser
  Parser
  (Maybe String
   -> Bool
   -> [String]
   -> Maybe Text
   -> Bool
   -> Bool
   -> Maybe Bool
   -> Bool
   -> Bool
   -> GhciOpts)
-> Parser (Maybe String)
-> Parser
     (Bool
      -> [String]
      -> Maybe Text
      -> Bool
      -> Bool
      -> Maybe Bool
      -> Bool
      -> Bool
      -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser String -> Parser (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Mod OptionFields String -> Parser String
forall s. IsString s => Mod OptionFields s -> Parser s
strOption
        (  String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"with-ghc"
        Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"GHC"
        Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields String
forall (f :: * -> *) a. String -> Mod f a
help String
"Use this GHC to run GHCi."
        ))
  Parser
  (Bool
   -> [String]
   -> Maybe Text
   -> Bool
   -> Bool
   -> Maybe Bool
   -> Bool
   -> Bool
   -> GhciOpts)
-> Parser Bool
-> Parser
     ([String]
      -> Maybe Text
      -> Bool
      -> Bool
      -> Maybe Bool
      -> Bool
      -> Bool
      -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (     Bool -> Bool
not
        (Bool -> Bool) -> Parser Bool -> Parser Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> String -> String -> Mod FlagFields Bool -> Parser Bool
boolFlags Bool
True
              String
"load"
              String
"load modules on start-up."
              Mod FlagFields Bool
forall m. Monoid m => m
idm
      )
  Parser
  ([String]
   -> Maybe Text
   -> Bool
   -> Bool
   -> Maybe Bool
   -> Bool
   -> Bool
   -> GhciOpts)
-> Parser [String]
-> Parser
     (Maybe Text
      -> Bool -> Bool -> Maybe Bool -> Bool -> Bool -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [String]
packagesParser
  Parser
  (Maybe Text
   -> Bool -> Bool -> Maybe Bool -> Bool -> Bool -> GhciOpts)
-> Parser (Maybe Text)
-> Parser (Bool -> Bool -> Maybe Bool -> Bool -> Bool -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text -> Parser (Maybe Text)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Mod OptionFields Text -> Parser Text
textOption
        (  String -> Mod OptionFields Text
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"main-is"
        Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Text
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"TARGET"
        Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> Completer -> Mod OptionFields Text
forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer Completer
targetCompleter
        Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Text
forall (f :: * -> *) a. String -> Mod f a
help String
"Specify which target should contain the main module to load, \
                \such as for an executable for test suite or benchmark."
        ))
  Parser (Bool -> Bool -> Maybe Bool -> Bool -> Bool -> GhciOpts)
-> Parser Bool
-> Parser (Bool -> Maybe Bool -> Bool -> Bool -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
        (  String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"load-local-deps"
        Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Load all local dependencies of your targets."
        )
  -- TODO: deprecate this? probably useless.

  Parser (Bool -> Maybe Bool -> Bool -> Bool -> GhciOpts)
-> Parser Bool -> Parser (Maybe Bool -> Bool -> Bool -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
        (  String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"skip-intermediate-deps"
        Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Skip loading intermediate target dependencies."
        Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> Mod FlagFields Bool
forall (f :: * -> *) a. Mod f a
internal
        )
  Parser (Maybe Bool -> Bool -> Bool -> GhciOpts)
-> Parser (Maybe Bool) -> Parser (Bool -> Bool -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool -> Parser (Maybe Bool)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (String -> String -> Mod FlagFields Bool -> Parser Bool
boolFlagsNoDefault
        String
"package-hiding"
        String
"package hiding"
        Mod FlagFields Bool
forall m. Monoid m => m
idm)
  Parser (Bool -> Bool -> GhciOpts)
-> Parser Bool -> Parser (Bool -> GhciOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
        (  String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"no-build"
        Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Don't build before launching GHCi."
        Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> Mod FlagFields Bool
forall (f :: * -> *) a. Mod f a
internal
        )
  Parser (Bool -> GhciOpts) -> Parser Bool -> Parser GhciOpts
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
        (  String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"only-main"
        Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Only load and import the main module. If no main module, no \
                \modules will be loaded."
        )