Skip to content

Commit

Permalink
Add --version option
Browse files Browse the repository at this point in the history
fixes #112
  • Loading branch information
sideeffffect committed Jun 26, 2016
1 parent acb37c3 commit 982225a
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/Main.hs
@@ -1,5 +1,4 @@
--------------------------------------------------------------------------------
{-# LANGUAGE DeriveDataTypeable #-}
module Main
( main
) where
Expand All @@ -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
Expand All @@ -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" <>
Expand Down Expand Up @@ -63,11 +67,16 @@ parseStylishArgs = StylishArgs
OA.help "Input file(s)")


--------------------------------------------------------------------------------
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


--------------------------------------------------------------------------------
Expand All @@ -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"
Expand All @@ -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

0 comments on commit 982225a

Please sign in to comment.