diff options
author | Ondra Pelech <ondra.pelech@gmail.com> | 2016-06-26 16:53:51 +0200 |
---|---|---|
committer | Ondra Pelech <ondra.pelech@gmail.com> | 2016-06-26 17:23:02 +0200 |
commit | 982225a408ad98e48c098f230c5075d3154fa83d (patch) | |
tree | a6b0a2a42930e6bd044a6765265b99bb7d91e69d /src | |
parent | acb37c38736075c8164fbc372419cdb29493bc60 (diff) | |
download | stylish-haskell-982225a408ad98e48c098f230c5075d3154fa83d.tar.gz |
Add --version option
fixes https://github.com/jaspervdj/stylish-haskell/issues/112
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.hs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/Main.hs b/src/Main.hs index 32d4780..e64b291 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,5 +1,4 @@ -------------------------------------------------------------------------------- -{-# LANGUAGE DeriveDataTypeable #-} module Main ( main ) where @@ -21,7 +20,8 @@ import Language.Haskell.Stylish -------------------------------------------------------------------------------- data StylishArgs = StylishArgs - { saConfig :: Maybe FilePath + { saVersion :: Bool + , saConfig :: Maybe FilePath , saVerbose :: Bool , saDefaults :: Bool , saInPlace :: Bool @@ -33,7 +33,11 @@ data StylishArgs = StylishArgs -------------------------------------------------------------------------------- parseStylishArgs :: OA.Parser StylishArgs parseStylishArgs = StylishArgs - <$> OA.optional (OA.strOption $ + <$> OA.switch ( + OA.help "Show version information" <> + OA.long "version" <> + OA.hidden) + <*> OA.optional (OA.strOption $ OA.metavar "CONFIG" <> OA.help "Configuration file" <> OA.long "config" <> @@ -64,10 +68,15 @@ parseStylishArgs = StylishArgs -------------------------------------------------------------------------------- +stylishHaskellVersion :: String +stylishHaskellVersion = "stylish-haskell " <> showVersion Paths_stylish_haskell.version + + +-------------------------------------------------------------------------------- parserInfo :: OA.ParserInfo StylishArgs parserInfo = OA.info (OA.helper <*> parseStylishArgs) $ OA.fullDesc <> - OA.header ("stylish-haskell v" <> showVersion Paths_stylish_haskell.version) + OA.header stylishHaskellVersion -------------------------------------------------------------------------------- @@ -80,12 +89,15 @@ stylishHaskell :: StylishArgs -> IO () stylishHaskell sa = do unless (saNoUtf8 sa) $ mapM_ (`IO.hSetEncoding` IO.utf8) [IO.stdin, IO.stdout] - case saDefaults sa of - True -> do + if saVersion sa then + putStrLn stylishHaskellVersion + + else if saDefaults sa then do fileName <- defaultConfigFilePath verbose' $ "Dumping config from " ++ fileName readUTF8File fileName >>= putStr - False -> do + + else do conf <- loadConfig verbose' (saConfig sa) let steps = configSteps conf forM_ steps $ \s -> verbose' $ "Enabled " ++ stepName s ++ " step" @@ -111,12 +123,11 @@ file sa conf mfp = do write old new = case mfp of Nothing -> putStr new Just _ | not (saInPlace sa) -> putStr new - Just path | length new /= 0 && old /= new -> writeFile path new + Just path | not (null new) && old /= new -> writeFile path new _ -> return () readUTF8File :: FilePath -> IO String readUTF8File fp = IO.withFile fp IO.ReadMode $ \h -> do IO.hSetEncoding h IO.utf8 - content <- IO.Strict.hGetContents h - return content + IO.Strict.hGetContents h |