rework the cli interface

pull/60/head
d-dorazio 2017-10-03 23:32:36 +02:00
parent a0112524aa
commit f21c6b6eac
3 changed files with 36 additions and 28 deletions

View File

@ -102,7 +102,7 @@ library {
, pretty >=1.1.3.3 && <1.2 , pretty >=1.1.3.3 && <1.2
, bytestring >=0.10.8.1 && <0.11 , bytestring >=0.10.8.1 && <0.11
, directory >=1.2.6.2 && <1.4 , directory >=1.2.6.2 && <1.4
, butcher >=1.1.0.0 && <1.2 , butcher >=1.2 && <1.3
, yaml >=0.8.18 && <0.9 , yaml >=0.8.18 && <0.9
, aeson >=1.0.1.0 && <1.3 , aeson >=1.0.1.0 && <1.3
, extra >=1.4.10 && <1.7 , extra >=1.4.10 && <1.7
@ -147,7 +147,7 @@ executable brittany
other-modules: { other-modules: {
Paths_brittany Paths_brittany
} }
-- other-extensions: -- other-extensions:
build-depends: build-depends:
{ brittany { brittany
, base , base
@ -335,7 +335,7 @@ test-suite littests
} }
ghc-options: -Wall ghc-options: -Wall
main-is: Main.hs main-is: Main.hs
other-modules: other-modules:
hs-source-dirs: src-literatetests hs-source-dirs: src-literatetests
default-extensions: { default-extensions: {
CPP CPP
@ -379,7 +379,7 @@ test-suite libinterfacetests
} }
ghc-options: -Wall ghc-options: -Wall
main-is: Main.hs main-is: Main.hs
other-modules: other-modules:
hs-source-dirs: src-libinterfacetests hs-source-dirs: src-libinterfacetests
default-extensions: { default-extensions: {
FlexibleContexts FlexibleContexts

View File

@ -12,8 +12,12 @@ import qualified Language.Haskell.GHC.ExactPrint.Types as ExactPrint.Types
import qualified Language.Haskell.GHC.ExactPrint.Parsers as ExactPrint.Parsers import qualified Language.Haskell.GHC.ExactPrint.Parsers as ExactPrint.Parsers
import qualified Data.Map as Map import qualified Data.Map as Map
import Text.Read (Read(..))
import qualified Text.ParserCombinators.ReadP as ReadP
import qualified Text.ParserCombinators.ReadPrec as ReadPrec
import qualified Data.Text.Lazy.Builder as Text.Builder import qualified Data.Text.Lazy.Builder as Text.Builder
import Control.Monad (zipWithM)
import Data.CZipWith import Data.CZipWith
import qualified Debug.Trace as Trace import qualified Debug.Trace as Trace
@ -39,6 +43,17 @@ import qualified GHC.LanguageExtensions.Type as GHC
import Paths_brittany import Paths_brittany
data WriteMode = Display | Inplace
instance Read WriteMode where
readPrec = val "display" Display <|> val "inplace" Inplace
where
val iden v = ReadPrec.lift $ ReadP.string iden >> return v
instance Show WriteMode where
show Display = "display"
show Inplace = "inplace"
main :: IO () main :: IO ()
main = mainFromCmdParserWithHelpDesc mainCmdParser main = mainFromCmdParserWithHelpDesc mainCmdParser
@ -103,8 +118,6 @@ mainCmdParser helpDesc = do
printHelp <- addSimpleBoolFlag "h" ["help"] mempty printHelp <- addSimpleBoolFlag "h" ["help"] mempty
printVersion <- addSimpleBoolFlag "" ["version"] mempty printVersion <- addSimpleBoolFlag "" ["version"] mempty
printLicense <- addSimpleBoolFlag "" ["license"] mempty printLicense <- addSimpleBoolFlag "" ["license"] mempty
inputPaths <- addFlagStringParams "i" ["input"] "PATH" (flagHelpStr "paths to input haskell source files")
outputPaths <- addFlagStringParams "o" ["output"] "PATH" (flagHelpStr "output file paths")
configPaths <- addFlagStringParams "" ["config-file"] "PATH" (flagHelpStr "path to config file") -- TODO: allow default on addFlagStringParam ? configPaths <- addFlagStringParams "" ["config-file"] "PATH" (flagHelpStr "path to config file") -- TODO: allow default on addFlagStringParam ?
cmdlineConfig <- configParser cmdlineConfig <- configParser
suppressOutput <- addSimpleBoolFlag suppressOutput <- addSimpleBoolFlag
@ -112,10 +125,17 @@ mainCmdParser helpDesc = do
["suppress-output"] ["suppress-output"]
(flagHelp $ parDoc "suppress the regular output, i.e. the transformed haskell source") (flagHelp $ parDoc "suppress the regular output, i.e. the transformed haskell source")
_verbosity <- addSimpleCountFlag "v" ["verbose"] (flagHelp $ parDoc "[currently without effect; TODO]") _verbosity <- addSimpleCountFlag "v" ["verbose"] (flagHelp $ parDoc "[currently without effect; TODO]")
inplace <- addSimpleBoolFlag "" ["inplace"] (flagHelp $ parDoc "overwrite the input files") writeMode <- addFlagReadParam
""
["write-mode"]
""
Flag
{ _flag_help = Just (PP.text "output mode: [display|inplace]")
, _flag_default = Just Display
}
reorderStop reorderStop
inputParam <- addStringParamOpt "PATH" (paramHelpStr "path to input haskell source file") inputParams <- addParamNoFlagStrings "PATH" (paramHelpStr "paths to input haskell source files")
desc <- peekCmdDesc desc <- peekCmdDesc
addCmdImpl $ void $ do addCmdImpl $ void $ do
when printLicense $ do when printLicense $ do
print licenseDoc print licenseDoc
@ -130,21 +150,10 @@ mainCmdParser helpDesc = do
liftIO $ print $ ppHelpShallow desc liftIO $ print $ ppHelpShallow desc
System.Exit.exitSuccess System.Exit.exitSuccess
let inputPaths' = case maybeToList inputParam ++ inputPaths of let inputPaths = if null inputParams then [Nothing] else map Just inputParams
[] -> [Nothing] let outputPaths = case writeMode of
ps -> map Just ps Display -> repeat Nothing
Inplace -> inputPaths
outputPaths' <- case outputPaths of
[] | not inplace -> return [Nothing]
[] -> return inputPaths'
ps | not inplace -> return . map Just $ ps
_ -> do
putStrErrLn "cannot specify output files and inplace at the same time"
System.Exit.exitWith (System.Exit.ExitFailure 51)
when (length inputPaths' /= length outputPaths') $ do
putStrErrLn "the number of inputs must match ther number of outputs"
System.Exit.exitWith (System.Exit.ExitFailure 52)
config <- runMaybeT (readConfigs cmdlineConfig configPaths) >>= \case config <- runMaybeT (readConfigs cmdlineConfig configPaths) >>= \case
Nothing -> System.Exit.exitWith (System.Exit.ExitFailure 53) Nothing -> System.Exit.exitWith (System.Exit.ExitFailure 53)
@ -152,9 +161,8 @@ mainCmdParser helpDesc = do
when (confUnpack $ _dconf_dump_config $ _conf_debug $ config) $ do when (confUnpack $ _dconf_dump_config $ _conf_debug $ config) $ do
trace (showConfigYaml config) $ return () trace (showConfigYaml config) $ return ()
let ios = zipWith (coreIO putStrErrLn config suppressOutput) inputPaths' outputPaths' results <- zipWithM (coreIO putStrErrLn config suppressOutput) inputPaths outputPaths
res <- fmap sequence_ $ sequence ios case sequence_ results of
case res of
Left _ -> System.Exit.exitWith (System.Exit.ExitFailure 1) Left _ -> System.Exit.exitWith (System.Exit.ExitFailure 1)
Right _ -> pure () Right _ -> pure ()

View File

@ -3,7 +3,7 @@ resolver: lts-9.0
extra-deps: extra-deps:
- monad-memo-0.4.1 - monad-memo-0.4.1
- czipwith-1.0.0.0 - czipwith-1.0.0.0
- butcher-1.1.0.2 - butcher-1.2.0.0
- data-tree-print-0.1.0.0 - data-tree-print-0.1.0.0
- deque-0.2 - deque-0.2