packages feed

ghcup-0.2.7.0 / lib-opt / GHCup / OptParse.hs

{-# LANGUAGE CPP               #-}{-# LANGUAGE DataKinds         #-}{-# LANGUAGE FlexibleContexts  #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE DuplicateRecordFields #-}module GHCup.OptParse (    module GHCup.OptParse.Common  , module GHCup.OptParse.Install  , module GHCup.OptParse.Test  , module GHCup.OptParse.TUI  , module GHCup.OptParse.Set  , module GHCup.OptParse.UnSet  , module GHCup.OptParse.Rm  , module GHCup.OptParse.Compile  , module GHCup.OptParse.Config  , module GHCup.OptParse.Whereis  , module GHCup.OptParse.List#ifndef DISABLE_UPGRADE  , module GHCup.OptParse.Upgrade#endif  , module GHCup.OptParse.ChangeLog  , module GHCup.OptParse.Prefetch  , module GHCup.OptParse.GC#if defined(DHALL)  , module GHCup.OptParse.Generate#endif  , module GHCup.OptParse.HealthCheck  , module GHCup.OptParse.Fixup  , module GHCup.OptParse.DebugInfo  , module GHCup.OptParse.Nuke  , module GHCup.OptParse.ToolRequirements  , module GHCup.OptParse.Run  , module GHCup.OptParse) whereimport           GHCup.OptParse.Commonimport           GHCup.OptParse.Installimport           GHCup.OptParse.Testimport           GHCup.OptParse.TUIimport           GHCup.OptParse.Setimport           GHCup.OptParse.UnSetimport           GHCup.OptParse.Rmimport           GHCup.OptParse.Runimport           GHCup.OptParse.Compileimport           GHCup.OptParse.Configimport           GHCup.OptParse.Whereisimport           GHCup.OptParse.List#ifndef DISABLE_UPGRADEimport           GHCup.OptParse.Upgrade#endifimport           GHCup.OptParse.ChangeLogimport           GHCup.OptParse.Prefetchimport           GHCup.OptParse.GC#if defined(DHALL)import           GHCup.OptParse.Generate#endifimport           GHCup.OptParse.HealthCheckimport           GHCup.OptParse.Fixupimport           GHCup.OptParse.DebugInfoimport           GHCup.OptParse.ToolRequirementsimport           GHCup.OptParse.Nukeimport           GHCup.Typesimport           GHCup.Input.Parsers (gpgParser, downloaderParser, keepOnParser, platformParser)#if !MIN_VERSION_base(4,13,0)import           Control.Monad.Fail             ( MonadFail )#endifimport           Data.Functorimport           Data.Maybeimport           Options.Applicative     hiding ( style )import           Options.Applicative.Pretty.Shim ( text )import           Prelude                 hiding ( appendFile )import System.Exitimport System.Environment (getProgName)import System.IOimport GHCup.Compat.Pagerimport qualified Data.Text as Timport Data.Function ((&))data Options = Options  {  -- global options    optVerbose     :: Maybe Int  , optCache       :: Maybe Bool  , optMetaCache   :: Maybe Integer  , optMetaMode    :: Maybe MetaMode  , optPlatform    :: Maybe PlatformRequest  , optUrlSource   :: Maybe [NewURLSource]  , optNoVerify    :: Maybe Bool  , optKeepDirs    :: Maybe KeepDirs  , optsDownloader :: Maybe Downloader  , optNoNetwork   :: Maybe Bool  , optGpg         :: Maybe GPGSetting  , optStackSetup  :: Maybe Bool  , optPager       :: Maybe Bool  , optGuessVersion :: Maybe Bool  -- commands  , optCommand     :: Command  }data Command  = Install InstallCommand  | Test TestCommand  | Set SetCommand  | UnSet UnsetCommand  | List ListOptions  | Rm RmCommand  | DInfo  | Compile CompileCommand  | Config ConfigCommand  | Whereis WhereisOptions WhereisCommand#ifndef DISABLE_UPGRADE  | Upgrade UpgradeOpts Bool Bool#endif  | ToolRequirements ToolReqOpts  | ChangeLog ChangeLogOptions#if defined(DHALL)  | Generate GenerateCommand#endif  | Nuke#if defined(BRICK)  | Interactive InteractiveOptions#endif  | Prefetch PrefetchCommand  | GC GCOptions  | Run RunOptions  | HealthCheck HealthCheckCommand  | Fixup FixupCommand  | PrintAppErrorstoVerbosity :: Maybe Bool -> Maybe InttoVerbosity (Just True)  = Just 1toVerbosity (Just False) = Just 0toVerbosity _           = Nothingopts :: Parser Optionsopts =  Options    <$> (fmap toVerbosity (invertableSwitch "verbose" (Just 'v') False (help "Enable verbosity (default: disabled)"))        <|> optional (option auto (long "verbosity" <> metavar "LEVEL" <> help "verbosity level (0 for off, 1 for 'verbose', 2 for extra)")))    <*> invertableSwitch "cache" (Just 'c') False (help "Cache downloads in ~/.ghcup/cache (default: disabled)")    <*> optional (option auto (long "metadata-caching" <> metavar "SEC" <> help "How long the yaml metadata caching interval is (in seconds), 0 to disable"))    <*> optional (option auto (long "metadata-fetching-mode" <> metavar "<Strict|Lax>" <> help "Whether to fail on metadata download failure (Strict) or fall back to cached version (Lax (default))"))    <*> optional      (option        (eitherReader platformParser)        (  short 'p'        <> long "platform"        <> metavar "PLATFORM"        <> help             "Override for platform (triple matching ghc tarball names), e.g. x86_64-fedora27-linux"        )      )    <*> optional          parseUrlSourceP    <*> (fmap . fmap) not (invertableSwitch "verify" (Just 'n') True (help "Disable tarball checksum verification (default: enabled)"))    <*> optional (option          (eitherReader keepOnParser)          (  long "keep"          <> metavar "<always|errors|never>"          <> help               "Keep build directories? (default: errors)"          <> hidden          <> completer (listCompleter ["always", "errors", "never"])          ))    <*> optional (option          (eitherReader downloaderParser)          (  long "downloader"#if defined(INTERNAL_DOWNLOADER)          <> metavar "<internal|curl|wget>"          <> help          "Downloader to use (default: internal)"          <> completer (listCompleter ["internal", "curl", "wget"])#else          <> metavar "<curl|wget>"          <> help          "Downloader to use (default: curl)"          <> completer (listCompleter ["curl", "wget"])#endif          <> hidden          ))    <*> invertableSwitch "offline" (Just 'o') False (help "Don't do any network calls, trying cached assets and failing if missing.")    <*> optional (option          (eitherReader gpgParser)          (  long "gpg"          <> metavar "<strict|lax|none>"          <> help          "GPG verification (default: none)"          <> completer (listCompleter ["strict", "lax", "none"])          ))    <*> invertableSwitch "stack-setup" Nothing False (help "Use stack's setup info for discovering and installing GHC versions")    <*> invertableSwitch "paginate" Nothing False (help "Send output (e.g. from 'ghcup list') through pager (default: disabled)")    <*> invertableSwitch "guess-version" Nothing True (help "Whether to guess the full version from an incomplete tool version, e.g. GHC '9.12' resolving to '9.12.2'")    <*> comcom :: Parser Commandcom =  subparser#if defined(BRICK)      (  command          "tui"          (   Interactive          <$> info                (interactiveParser <**> helper)                (  progDesc "Start the interactive GHCup UI"                )          )      <>  command#else      (  command#endif          "install"          (   Install          <$> info                (installParser <**> helper)                (  progDesc "Install or update GHC/cabal/HLS/stack"                <> footerDoc (Just $ text installToolFooter)                )          )      <> command           "test"           (info             (Test <$> testParser <**> helper)             (  progDesc "Run tests for a tool (if any) [EXPERIMENTAL!]"             <> footerDoc (Just $ text testFooter)             )           )      <> command           "set"           (info             (Set <$> setParser <**> helper)             (  progDesc "Set currently active GHC/cabal version"             <> footerDoc (Just $ text setFooter)             )           )      <> command           "unset"           (info             (UnSet <$> unsetParser <**> helper)             (  progDesc "Unset currently active GHC/cabal version"             <> footerDoc (Just $ text unsetFooter)             )           )      <> command           "rm"           (info             (Rm <$> rmParser <**> helper)             (  progDesc "Remove a GHC/cabal/HLS/stack version"             <> footerDoc (Just $ text rmFooter)             )           )      <> command           "list"           (info (List <$> listOpts <**> helper)                 (progDesc "Show available GHCs and other tools"                 <> footerDoc (Just $ text listToolFooter))           )      <> command           "upgrade"           (info             (    (Upgrade <$> upgradeOptsP <*> switch                    (short 'f' <> long "force" <> help "Force update")                    <*> switch                    (long "fail-if-shadowed" <> help "Fails after upgrading if the upgraded ghcup binary is shadowed by something else in PATH (useful for CI)")                  )             <**> helper             )             (progDesc "Upgrade ghcup")           )      <> command           "compile"           (   Compile           <$> info (compileP <**> helper)                    (progDesc "Compile a tool from source")           )      <> command           "whereis"            (info             (                     (Whereis . WhereisOptions <$> switch (short 'd' <> long "directory" <> help "return directory of the binary instead of the binary location")                     <*> whereisP                 ) <**> helper             )             (progDesc "Find a tools location"             <> footerDoc ( Just $ text whereisFooter ))           )      <> command           "prefetch"            (info             (   (Prefetch                     <$> prefetchP                 ) <**> helper             )             (progDesc "Prefetch assets"             <> footerDoc ( Just $ text prefetchFooter ))           )      <> command           "gc"            (info             (   (GC                     <$> gcP                 ) <**> helper             )             (progDesc "Garbage collection"             <> footerDoc ( Just $ text gcFooter ))           )      <> command              "run"               (Run               <$>                 info                   (runOpts <**> helper)                   (progDesc "Run a command with the given tool in PATH"                   <> footerDoc ( Just $ text runFooter )                   )               )      <> command              "check"              ( HealthCheck <$>               info (hcP <**> helper)                    (progDesc "Health check ghcup")              )      <> command              "fixup"              ( Fixup <$>               info (fixupP <**> helper)                    (progDesc "Fix up issues with installations")              )      <> commandGroup "Main commands:"      )    <|> subparser          (  command              "debug-info"              ((\_ -> DInfo) <$> info helper (progDesc "Show debug info"))          <> command               "tool-requirements"               (   ToolRequirements               <$> info (toolReqP <**> helper)                        (progDesc "Show the requirements for ghc/cabal")               )          <> command               "changelog"               (info                  (fmap ChangeLog changelogP <**> helper)                  (  progDesc "Find/show changelog"                  <> footerDoc (Just $ text changeLogFooter)                  )               )          <> command               "config"               (   Config               <$> info (configP <**> helper)                        (progDesc "Show or set config" <> footerDoc (Just $ text configFooter))               )#if defined(DHALL)          <> command               "generate"                ( Generate                <$> (info (generateP <**> helper)                          (progDesc "Various generation facilities related to metadata"))                )#endif          <> command               "nuke"                (info (pure Nuke <**> helper)                      (progDesc "Completely remove ghcup from your system"))          <> commandGroup "Other commands:"          <> hidden          )     <|> subparser          (command              "print-app-errors"               (info (pure PrintAppErrors <**> helper)                     (progDesc ""))           <> internal          )-- | Handle 'ParserResult'.handleParseResult' :: Maybe FilePath -> Bool -> ParserResult a -> IO ahandleParseResult' _ _ (Success a) = return ahandleParseResult' pagerCmd hasHelp (Failure failure) = do      progn <- getProgName      let (msg, exit) = renderFailure failure progn      case exit of        ExitSuccess          | hasHelp -> sendToPager' pagerCmd (T.lines $ T.pack msg)          | otherwise -> putStrLn msg        _           -> hPutStrLn stderr msg      exitWith exithandleParseResult' _ _ (CompletionInvoked compl) = do      progn <- getProgName      msg <- execCompletion compl progn      putStr msg      exitSuccess-- | Checks whether any non-longopts args are '--help'.argsHasHelp :: [String] -> BoolargsHasHelp args = takeWhile (/= "--") args & elem "--help"