diff --git a/README.md b/README.md index 82f3e6b..3b028ba 100644 --- a/README.md +++ b/README.md @@ -55,17 +55,17 @@ In order to compile a Nirum package (`examples/`) to a Python package: For more infomration, use `--help` option: $ nirum --help - Nirum Compiler 0.3.0 + Nirum: The IDL compiler and RPC/distributed object framework - nirum [OPTIONS] DIR - - Common flags: - -o --output-dir=DIR The directory to place object files - -t --target=TARGET The target language. Available targets: python - -? --help Display help message - -V --version Print version information - --numeric-version Print just the version number + Usage: nirum [-v|--version] (-o|--output-dir DIR) (-t|--target TARGET) DIR + Nirum compiler 0.3.0 + Available options: + -h,--help Show this help text + -v,--version Show version + -o,--output-dir DIR Output directory + -t,--target TARGET Target language name + DIR Package directory Building -------- diff --git a/appveyor.yml b/appveyor.yml index 2d0211b..ac02309 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -40,7 +40,7 @@ after_build: test_script: - stack test # FIXME: duplicate code with test.sh -- stack exec -- nirum -o nirum_fixture test/nirum_fixture +- stack exec -- nirum -o nirum_fixture -t python test/nirum_fixture - C:\Python35\Scripts\tox artifacts: - path: nirum-win-x86.exe diff --git a/nirum.cabal b/nirum.cabal index 40ac592..cda5c86 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 diff --git a/src/Nirum/Cli.hs b/src/Nirum/Cli.hs index 8dcf3a9..4f27b45 100644 --- a/src/Nirum/Cli.hs +++ b/src/Nirum/Cli.hs @@ -1,28 +1,15 @@ -{-# LANGUAGE ExtendedDefaultRules, QuasiQuotes, DeriveDataTypeable #-} +{-# LANGUAGE ExtendedDefaultRules, QuasiQuotes #-} 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.Exit (die) import System.FilePath (takeDirectory, ()) @@ -57,11 +44,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 @@ -128,24 +110,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') -> @@ -181,5 +150,39 @@ writeFiles outDir files = putStrLn outPath B.writeFile outPath code +data Opts = Opts { outDirectory :: !String + , targetOption :: !String + , packageDirectory :: !String + } + main :: IO () -main = catchIOError main' $ die . 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 " ++ versionString) <> + 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 "output-dir" <> OPT.short 'o' <> OPT.metavar "DIR" <> + OPT.help "Output directory") <*> + OPT.strOption + (OPT.long "target" <> OPT.short 't' <> OPT.metavar "TARGET" <> + OPT.help "Target language name") <*> + OPT.strArgument + (OPT.metavar "DIR" <> OPT.help "Package directory") diff --git a/test.sh b/test.sh index 58d2ea0..4c466da 100755 --- a/test.sh +++ b/test.sh @@ -2,6 +2,6 @@ set -e stack build -stack exec -- nirum -o nirum_fixture test/nirum_fixture +stack exec -- nirum -o nirum_fixture -t python test/nirum_fixture tox --skip-missing-interpreters