@@ -13,6 +13,7 @@ import Options.Applicative
13import Ormolu
13import Ormolu
14import Paths_ormolu (version)
14import Paths_ormolu (version)
15import System.Exit (ExitCode (..), exitWith)
15import System.Exit (ExitCode (..), exitWith)
16+import System.IO (hPutStrLn, stderr)
16import qualified Data.Text.IO as TIO
17import qualified Data.Text.IO as TIO
17import qualified Data.Yaml as Yaml
18import qualified Data.Yaml as Yaml
@@ -26,18 +27,30 @@ main = withPrettyOrmoluExceptions $ do
26 Just path -> do
27 Just path -> do
27 config <- Yaml.decodeFileThrow path
28 config <- Yaml.decodeFileThrow path
28 return (config <> optConfig)
29 return (config <> optConfig)
30+ r <- case optInputFile of
31+ "-" -> ormoluStdin config
32+ inputFile -> ormoluFile config inputFile
29− r <- ormoluFile config optInputFile
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)
30 case optMode of
40 case optMode of
32 TIO.putStr r
42 TIO.putStr r
33− InPlace ->
43+ InPlace -> do
34 TIO.writeFile optInputFile r
45 TIO.writeFile optInputFile r
35 Check -> do
46 Check -> do
36 r' <- TIO.readFile optInputFile
48 r' <- TIO.readFile optInputFile
37− when (r /= r') . exitWith $
49+ when (r /= r') $
38− ExitFailure 100 -- 100 is different to all the other exit code that
50+ -- 100 is different to all the other exit code that are emitted
39− -- are emitted either from an 'OrmoluException' or
51+ -- either from an 'OrmoluException' or from 'error' and
40− -- from 'error' and 'notImplemented'.
52+ -- 'notImplemented'.
53+ exitWith (ExitFailure 100)
42----------------------------------------------------------------------------
55----------------------------------------------------------------------------
43-- Command line options parsing.
56-- Command line options parsing.
@@ -50,7 +63,7 @@ data Opts = Opts
50 , optConfig :: !Config
63 , optConfig :: !Config
51 -- ^ Ormolu 'Config'
64 -- ^ Ormolu 'Config'
52 , optInputFile :: !FilePath
65 , optInputFile :: !FilePath
53− -- ^ Input source file
66+ -- ^ Input source file or stdin ("-")
56-- | Mode of operation.
69-- | Mode of operation.
@@ -103,7 +116,8 @@ optsParser = Opts
103 <*> configParser
116 <*> configParser
104 <*> (strArgument . mconcat)
117 <*> (strArgument . mconcat)
105 [ metavar "FILE"
118 [ metavar "FILE"
106− , help "Haskell source file to format"
120+ , help "Haskell source file to format or stdin (default)"
109configParser :: Parser Config
123configParser :: Parser Config