@@ -13,6 +13,7 @@ import Options.Applicative
1313import Ormolu
1414import Paths_ormolu (version)
1515import System.Exit (ExitCode (..), exitWith)
16+import System.IO (hPutStrLn, stderr)
1617import qualified Data.Text.IO as TIO
1718import qualified Data.Yaml as Yaml
1819
@@ -26,18 +27,30 @@ main = withPrettyOrmoluExceptions $ do
2627 Just path -> do
2728 config <- Yaml.decodeFileThrow path
2829 return (config <> optConfig)
29− r <- ormoluFile config optInputFile
30+ r <- case optInputFile of
31+ "-" -> ormoluStdin config
32+ inputFile -> ormoluFile config inputFile
33+ let notForStdin = do
34+ when (optInputFile == "-") $ do
35+ hPutStrLn
36+ stderr
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)
3040 case optMode of
3141 Stdout ->
3242 TIO.putStr r
33− InPlace ->
43+ InPlace -> do
44+ notForStdin
3445 TIO.writeFile optInputFile r
3546 Check -> do
47+ notForStdin
3648 r' <- TIO.readFile optInputFile
37− when (r /= r') . exitWith $
38− ExitFailure 100 -- 100 is different to all the other exit code that
39− -- are emitted either from an 'OrmoluException' or
40− -- from 'error' and 'notImplemented'.
49+ when (r /= r') $
50+ -- 100 is different to all the other exit code that are emitted
51+ -- either from an 'OrmoluException' or from 'error' and
52+ -- 'notImplemented'.
53+ exitWith (ExitFailure 100)
4154
4255----------------------------------------------------------------------------
4356-- Command line options parsing.
@@ -50,7 +63,7 @@ data Opts = Opts
5063 , optConfig :: !Config
5164 -- ^ Ormolu 'Config'
5265 , optInputFile :: !FilePath
53− -- ^ Input source file
66+ -- ^ Input source file or stdin ("-")
5467 }
5568
5669-- | Mode of operation.
@@ -103,7 +116,8 @@ optsParser = Opts
103116 <*> configParser
104117 <*> (strArgument . mconcat)
105118 [ metavar "FILE"
106− , help "Haskell source file to format"
119+ , value "-"
120+ , help "Haskell source file to format or stdin (default)"
107121 ]
108122
109123configParser :: Parser Config