2{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE TemplateHaskell #-}
6module Main (main) where
9import Data.List (intercalate)
10import Data.Version (showVersion)
11import Development.GitRev
12import Options.Applicative
14import Paths_ormolu (version)
15import System.Exit (ExitCode (..), exitWith)
16import System.IO (hPutStrLn, stderr)
17import qualified Data.Text.IO as TIO
18import qualified Data.Yaml as Yaml
20-- | Entry point of the program.
23main = withPrettyOrmoluExceptions $ do
24 Opts {..} <- execParser optsParserInfo
25 config <- case optConfigFile of
26 Nothing -> return optConfig
28 config <- Yaml.decodeFileThrow path
29 return (config <> optConfig)
30 r <- case optInputFile of
31 "-" -> ormoluStdin config
32 inputFile -> ormoluFile config inputFile
34 when (optInputFile == "-") $ do
37 "This feature is not supported when input comes from stdin."
38 -- 101 is different from all the other exit codes we already use.
39 exitWith (ExitFailure 101)
45 TIO.writeFile optInputFile r
48 r' <- TIO.readFile optInputFile
50 -- 100 is different to all the other exit code that are emitted
51 -- either from an 'OrmoluException' or from 'error' and
53 exitWith (ExitFailure 100)
55----------------------------------------------------------------------------
56-- Command line options parsing.
60 -- ^ Mode of operation
61 , optConfigFile :: !(Maybe FilePath)
62 -- ^ Location of configuration file (optional)
63 , optConfig :: !Config
65 , optInputFile :: !FilePath
66 -- ^ Input source file or stdin ("-")
69-- | Mode of operation.
72 = Stdout -- ^ Output formatted source code to stdout
73 | InPlace -- ^ Overwrite original file
74 | Check -- ^ Exit with non-zero status code if
75 -- source is not already formatted
78optsParserInfo :: ParserInfo Opts
79optsParserInfo = info (helper <*> ver <*> optsParser) . mconcat $
85 ver :: Parser (a -> a)
86 ver = infoOption verStr . mconcat $
89 , help "Print version of the program"
91 verStr = intercalate "\n"
98 , "using ghc " ++ VERSION_ghc
101optsParser :: Parser Opts
103 <$> (option parseMode . mconcat)
108 , help "Mode of operation: 'stdout', 'inplace', or 'check'"
110 <*> (optional . strOption . mconcat)
114 , help "Location of configuration file"
117 <*> (strArgument . mconcat)
120 , help "Haskell source file to format or stdin (default)"
123configParser :: Parser Config
125 <$> (fmap (fmap DynOption) . many . strOption . mconcat)
129 , help "GHC options to enable (e.g. language extensions)"
131 <*> (switch . mconcat)
134 , help "Do formatting faster but without automatic detection of defects"
136 <*> (switch . mconcat)
139 , help "Output information useful for debugging"
142----------------------------------------------------------------------------
147parseMode :: ReadM Mode
148parseMode = eitherReader $ \case
149 "stdout" -> Right Stdout
150 "inplace" -> Right InPlace
151 "check" -> Right Check
152 s -> Left $ "unknown mode: " ++ s
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list