diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2012-05-29 18:07:20 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2012-05-29 18:07:20 +0200 |
commit | ba19562257064f529886f7f4f85ff5b2fdf46624 (patch) | |
tree | 993188d708f3e812e7cf4f1ebd6d06d897f7f5e3 | |
parent | f2cd0b967bae6fbbee875f02dfbe0231463e2f1c (diff) | |
download | stylish-haskell-ba19562257064f529886f7f4f85ff5b2fdf46624.tar.gz |
Parse command line arguments using cmdargs
-rw-r--r-- | src/Main.hs | 28 | ||||
-rw-r--r-- | src/StylishHaskell.hs | 22 | ||||
-rw-r--r-- | stylish-haskell.cabal | 3 |
3 files changed, 39 insertions, 14 deletions
diff --git a/src/Main.hs b/src/Main.hs index 5a1c5e4..78dff11 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,42 +1,42 @@ -------------------------------------------------------------------------------- +{-# LANGUAGE DeriveDataTypeable #-} module Main ( main ) where -------------------------------------------------------------------------------- -import Control.Applicative ((<$>)) import Data.Maybe (listToMaybe) -import System.Environment (getArgs) +import System.Console.CmdArgs -------------------------------------------------------------------------------- +import StylishHaskell import qualified StylishHaskell.Imports import qualified StylishHaskell.LanguagePragmas -import StylishHaskell.Parse -import StylishHaskell.Stylish import qualified StylishHaskell.TrailingWhitespace -------------------------------------------------------------------------------- -runStylish :: Maybe FilePath -> Stylish -> Lines -> Lines -runStylish mfp f ls = case parseModule mfp (unlines ls) of - Left err -> error err -- TODO: maybe return original lines? - Right module' -> f ls module' +data StylishArgs = StylishArgs + { config :: Maybe FilePath + , files :: [FilePath] + } deriving (Data, Show, Typeable) -------------------------------------------------------------------------------- -chainStylish :: Maybe FilePath -> [Stylish] -> Lines -> Lines -chainStylish mfp filters = foldr (.) id filters' - where - filters' :: [Lines -> Lines] - filters' = map (runStylish mfp) filters +stylishArgs :: StylishArgs +stylishArgs = StylishArgs + { config = Nothing &= typFile &= help "Configuration file" + , files = [] &= typFile &= args + } &= summary "stylish-haskell" -------------------------------------------------------------------------------- main :: IO () main = do - filePath <- listToMaybe <$> getArgs + sa <- cmdArgs stylishArgs + let filePath = listToMaybe $ files sa contents <- maybe getContents readFile filePath putStr $ unlines $ chainStylish filePath filters $ lines contents where diff --git a/src/StylishHaskell.hs b/src/StylishHaskell.hs new file mode 100644 index 0000000..0b0bfa2 --- /dev/null +++ b/src/StylishHaskell.hs @@ -0,0 +1,22 @@ +-------------------------------------------------------------------------------- +module StylishHaskell + ( runStylish + , chainStylish + ) where + + +-------------------------------------------------------------------------------- +import StylishHaskell.Parse +import StylishHaskell.Stylish + + +-------------------------------------------------------------------------------- +runStylish :: Maybe FilePath -> Stylish -> Lines -> Lines +runStylish mfp f ls = case parseModule mfp (unlines ls) of + Left err -> error err -- TODO: maybe return original lines? + Right module' -> f ls module' + + +-------------------------------------------------------------------------------- +chainStylish :: Maybe FilePath -> [Stylish] -> Lines -> Lines +chainStylish mfp = foldr (.) id . map (runStylish mfp) diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index aebf0be..65bad5f 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -24,6 +24,7 @@ Executable stylish-haskell Main-is: Main.hs Other-modules: + StylishHaskell StylishHaskell.Block StylishHaskell.Editor StylishHaskell.Imports @@ -36,6 +37,7 @@ Executable stylish-haskell Build-depends: base >= 4 && < 5, + cmdargs >= 0.9 && < 0.10, haskell-src-exts >= 1.13 && < 1.14 Test-suite stylish-haskell-tests @@ -57,6 +59,7 @@ Test-suite stylish-haskell-tests test-framework-hunit >= 0.2 && < 0.3, -- Copied from regular dependencies... base >= 4 && < 5, + cmdargs >= 0.9 && < 0.10, haskell-src-exts >= 1.13 && < 1.14 Source-repository head |