From 6412618300ab6261e48727f39f79b6fcf0955de3 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 12:31:55 +0900 Subject: [PATCH] Use optparse-applicative instead of cmdargs --- nirum.cabal | 2 ++ src/Nirum/Cli.hs | 74 +++++++++++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/nirum.cabal b/nirum.cabal index 40ac592..fd33c43 100644 --- a/nirum.cabal +++ b/nirum.cabal @@ -58,6 +58,7 @@ library , interpolatedstring-perl6 >=1.0.0 && <1.1.0 , megaparsec >=5 && <5.3 , mtl >=2.2.1 && <3 + , optparse-applicative >=0.13.1 && <0.14 , parsec -- only for dealing with htoml's ParserError , semver >=0.3.0 && <1.0 @@ -133,6 +134,7 @@ test-suite spec , megaparsec , mtl , nirum + , optparse-applicative >=0.13.1 && <0.14 , parsec -- only for dealing with htoml's ParserError , process >=1.1 && <2 diff --git a/src/Nirum/Cli.hs b/src/Nirum/Cli.hs index ba0b639..571acb2 100644 --- a/src/Nirum/Cli.hs +++ b/src/Nirum/Cli.hs @@ -3,26 +3,13 @@ module Nirum.Cli (main, writeFiles) where import Control.Monad (forM_) import GHC.Exts (IsList (toList)) -import System.IO.Error (catchIOError, ioeGetErrorString) import qualified Data.ByteString as B import qualified Data.Map.Strict as M import qualified Data.Set as S import qualified Data.Text as T -import System.Console.CmdArgs.Implicit ( Data - , Typeable - , argPos - , cmdArgs - , explicit - , help - , name - , program - , summary - , typ - , typDir - , (&=) - ) -import System.Console.CmdArgs.Default (def) +import Data.Monoid +import qualified Options.Applicative as OPT import System.Directory (createDirectoryIfMissing) import System.FilePath (takeDirectory, ()) import Text.InterpolatedString.Perl6 (qq) @@ -56,11 +43,6 @@ import Nirum.Targets ( BuildError (CompileError, PackageError, TargetNameError) ) import Nirum.Version (versionString) -data NirumCli = NirumCli { sourcePath :: FilePath - , objectPath :: FilePath - , targetName :: TargetName - } deriving (Show, Data, Typeable) - parseErrortoPrettyMessage :: ParseError (Token T.Text) Dec -> FilePath -> IO String @@ -127,24 +109,11 @@ importErrorsToPrettyMessage importErrors = withListStyleText = map (T.append "- ") (importErrorsToMessageList importErrors) -nirumCli :: NirumCli -nirumCli = NirumCli - { objectPath = def &= explicit - &= name "o" &= name "output-dir" &= typDir - &= help "The directory to place object files" - , targetName = "python" &= explicit - &= name "t" &= name "target" &= typ "TARGET" - &= help ("The target language. Available targets: " ++ - T.unpack targetNamesText) - , sourcePath = def &= argPos 1 &= typDir - } &= program "nirum" &= summary ("Nirum Compiler " ++ versionString) - targetNamesText :: T.Text targetNamesText = T.intercalate ", " $ S.toAscList targetNames -main' :: IO () -main' = do - NirumCli src outDir target <- cmdArgs nirumCli +runCli :: FilePath -> FilePath -> TargetName -> IO () +runCli src outDir target = do result <- buildPackage target src case result of Left (TargetNameError targetName') -> @@ -180,5 +149,38 @@ writeFiles outDir files = putStrLn outPath B.writeFile outPath code +data Opts = Opts { outDirectory :: !String + , targetOption :: !String + , packageDirectory :: !String + } + main :: IO () -main = catchIOError main' $ putStrLn . ioeGetErrorString +main = do + opts <- OPT.execParser optsParser + let packageDirectoryPath = packageDirectory opts + outDirectoryPath = outDirectory opts + targetName = T.pack $ targetOption opts + runCli packageDirectoryPath outDirectoryPath targetName + where + optsParser :: OPT.ParserInfo Opts + optsParser = + OPT.info + (OPT.helper <*> versionOption <*> programOptions) + (OPT.fullDesc <> OPT.progDesc "Nirum compiler." <> + OPT.header header) + header :: String + header = "nirum - The IDL compiler and RPC/distributed object framework" + versionOption :: OPT.Parser (Opts -> Opts) + versionOption = OPT.infoOption + versionString (OPT.long "version" <> + OPT.short 'v' <> OPT.help "Show version") + programOptions :: OPT.Parser Opts + programOptions = + Opts <$> OPT.strOption + (OPT.long "outdir" <> OPT.short 'o' <> OPT.metavar "OUTDIR" <> + OPT.help "out directory") <*> + OPT.strOption + (OPT.long "target" <> OPT.short 't' <> OPT.metavar "TARGET" <> + OPT.help "Target name") <*> + OPT.strArgument + (OPT.metavar "PACKAGE" <> OPT.help "Package directory")