{-# LANGUAGE NoImplicitPrelude #-}

module Stack.Options.GhcBuildParser
( ghcBuildParser
) where

import           Options.Applicative
                   ( Parser, completeWith, help, long, metavar, option )
import           Options.Applicative.Types ( readerAsk, readerError )
import           Stack.Options.Utils ( hideMods )
import           Stack.Prelude
import           Stack.Types.CompilerBuild ( CompilerBuild, parseCompilerBuild )

-- | GHC build parser

ghcBuildParser :: Bool -> Parser CompilerBuild
ghcBuildParser :: Bool -> Parser CompilerBuild
ghcBuildParser Bool
hide = ReadM CompilerBuild
-> Mod OptionFields CompilerBuild -> Parser CompilerBuild
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM CompilerBuild
readGHCBuild
  (  String -> Mod OptionFields CompilerBuild
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"ghc-build"
  Mod OptionFields CompilerBuild
-> Mod OptionFields CompilerBuild -> Mod OptionFields CompilerBuild
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields CompilerBuild
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"BUILD"
  Mod OptionFields CompilerBuild
-> Mod OptionFields CompilerBuild -> Mod OptionFields CompilerBuild
forall a. Semigroup a => a -> a -> a
<> [String] -> Mod OptionFields CompilerBuild
forall (f :: * -> *) a. HasCompleter f => [String] -> Mod f a
completeWith
       [ String
"standard"
       , String
"gmp4"
       , String
"nopie"
       , String
"tinfo6"
       , String
"tinfo6-libc6-pre232"
       , String
"tinfo6-nopie"
       , String
"ncurses6"
       , String
"int-native"
       , String
"integersimple"
       ]
  Mod OptionFields CompilerBuild
-> Mod OptionFields CompilerBuild -> Mod OptionFields CompilerBuild
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields CompilerBuild
forall (f :: * -> *) a. String -> Mod f a
help String
"Specialized GHC build, e.g. 'gmp4' or 'standard' (usually \
          \auto-detected)."
  Mod OptionFields CompilerBuild
-> Mod OptionFields CompilerBuild -> Mod OptionFields CompilerBuild
forall a. Semigroup a => a -> a -> a
<> Bool -> Mod OptionFields CompilerBuild
forall (f :: * -> *) a. Bool -> Mod f a
hideMods Bool
hide
  )
 where
  readGHCBuild :: ReadM CompilerBuild
readGHCBuild = do
    String
s <- ReadM String
readerAsk
    case String -> Either SomeException CompilerBuild
forall (m :: * -> *). MonadThrow m => String -> m CompilerBuild
parseCompilerBuild String
s of
      Left SomeException
e -> String -> ReadM CompilerBuild
forall a. String -> ReadM a
readerError (SomeException -> String
forall e. Exception e => e -> String
displayException SomeException
e)
      Right CompilerBuild
v -> CompilerBuild -> ReadM CompilerBuild
forall a. a -> ReadM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CompilerBuild
v