summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2016-06-10 13:12:08 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2016-06-10 13:12:08 +0200
commitff5e8ad79925339548c9289c73ef038d3dead33f (patch)
treee148e15766abdfc9dc5ee198c8d91d975d5d2576 /src
parent90f18cb6899aaddcbce4b1afeb0e1ca5c3102fcf (diff)
downloadstylish-haskell-ff5e8ad79925339548c9289c73ef038d3dead33f.tar.gz
Use --utf8 by default, use optparse-applicative
Diffstat (limited to 'src')
-rw-r--r--src/Main.hs89
1 files changed, 58 insertions, 31 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 820d814..32d4780 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -6,12 +6,13 @@ module Main
--------------------------------------------------------------------------------
-import Control.Monad (forM_, when)
-import Data.List (intercalate)
-import Data.Version (Version (..))
-import System.Console.CmdArgs
+import Control.Monad (forM_, unless)
+import Data.Monoid ((<>))
+import Data.Version (showVersion)
+import qualified Options.Applicative as OA
+import qualified Paths_stylish_haskell
import qualified System.IO as IO
-import System.IO.Strict (hGetContents)
+import qualified System.IO.Strict as IO.Strict
--------------------------------------------------------------------------------
@@ -20,54 +21,80 @@ import Language.Haskell.Stylish
--------------------------------------------------------------------------------
data StylishArgs = StylishArgs
- { config :: Maybe FilePath
- , verbose :: Bool
- , defaults :: Bool
- , inPlace :: Bool
- , utf8 :: Bool
- , files :: [FilePath]
- } deriving (Data, Show, Typeable)
+ { saConfig :: Maybe FilePath
+ , saVerbose :: Bool
+ , saDefaults :: Bool
+ , saInPlace :: Bool
+ , saNoUtf8 :: Bool
+ , saFiles :: [FilePath]
+ } deriving (Show)
--------------------------------------------------------------------------------
-stylishArgs :: StylishArgs
-stylishArgs = StylishArgs
- { config = Nothing &= typFile &= help "Configuration file"
- , verbose = False &= help "Run in verbose mode"
- , defaults = False &= help "Dump default config and exit"
- , inPlace = False &= help "Overwrite the given files in place"
- , utf8 = False &= help "Force UTF-8 stdin/stdout"
- , files = [] &= typFile &= args
- } &= summary ("stylish-haskell-" ++ versionString version)
- where
- versionString = intercalate "." . map show . versionBranch
+parseStylishArgs :: OA.Parser StylishArgs
+parseStylishArgs = StylishArgs
+ <$> OA.optional (OA.strOption $
+ OA.metavar "CONFIG" <>
+ OA.help "Configuration file" <>
+ OA.long "config" <>
+ OA.short 'c' <>
+ OA.hidden)
+ <*> OA.switch (
+ OA.help "Run in verbose mode" <>
+ OA.long "verbose" <>
+ OA.short 'v' <>
+ OA.hidden)
+ <*> OA.switch (
+ OA.help "Dump default config and exit" <>
+ OA.long "defaults" <>
+ OA.short 'd' <>
+ OA.hidden)
+ <*> OA.switch (
+ OA.help "Overwrite the given files in place" <>
+ OA.long "inplace" <>
+ OA.short 'i' <>
+ OA.hidden)
+ <*> OA.switch (
+ OA.help "Don't force UTF-8 stdin/stdout" <>
+ OA.long "no-utf8" <>
+ OA.hidden)
+ <*> OA.many (OA.strArgument $
+ OA.metavar "FILENAME" <>
+ OA.help "Input file(s)")
+
+
+--------------------------------------------------------------------------------
+parserInfo :: OA.ParserInfo StylishArgs
+parserInfo = OA.info (OA.helper <*> parseStylishArgs) $
+ OA.fullDesc <>
+ OA.header ("stylish-haskell v" <> showVersion Paths_stylish_haskell.version)
--------------------------------------------------------------------------------
main :: IO ()
-main = cmdArgs stylishArgs >>= stylishHaskell
+main = OA.execParser parserInfo >>= stylishHaskell
--------------------------------------------------------------------------------
stylishHaskell :: StylishArgs -> IO ()
stylishHaskell sa = do
- when (utf8 sa) $
+ unless (saNoUtf8 sa) $
mapM_ (`IO.hSetEncoding` IO.utf8) [IO.stdin, IO.stdout]
- case defaults sa of
+ case saDefaults sa of
True -> do
fileName <- defaultConfigFilePath
verbose' $ "Dumping config from " ++ fileName
readUTF8File fileName >>= putStr
False -> do
- conf <- loadConfig verbose' (config sa)
+ conf <- loadConfig verbose' (saConfig sa)
let steps = configSteps conf
forM_ steps $ \s -> verbose' $ "Enabled " ++ stepName s ++ " step"
verbose' $ "Extra language extensions: " ++
show (configLanguageExtensions conf)
mapM_ (file sa conf) files'
where
- verbose' = makeVerbose (verbose sa)
- files' = if null (files sa) then [Nothing] else map Just (files sa)
+ verbose' = makeVerbose (saVerbose sa)
+ files' = if null (saFiles sa) then [Nothing] else map Just (saFiles sa)
--------------------------------------------------------------------------------
@@ -83,7 +110,7 @@ file sa conf mfp = do
where
write old new = case mfp of
Nothing -> putStr new
- Just _ | not (inPlace sa) -> putStr new
+ Just _ | not (saInPlace sa) -> putStr new
Just path | length new /= 0 && old /= new -> writeFile path new
_ -> return ()
@@ -91,5 +118,5 @@ readUTF8File :: FilePath -> IO String
readUTF8File fp =
IO.withFile fp IO.ReadMode $ \h -> do
IO.hSetEncoding h IO.utf8
- content <- IO.hGetContents h
+ content <- IO.Strict.hGetContents h
return content