From 85a5dc83561ccb06a90c91dac515798773278262 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 12:31:55 +0900 Subject: [PATCH 1/9] 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 8dcf3a9..d158bef 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.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,38 @@ 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." <> + 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") From d1ab100c4be9996f9d128e1be4dcc9fb45e74153 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 15:45:23 +0900 Subject: [PATCH 2/9] nirum require -t option to run --- test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 22cfc4689c48ca37b9e5b0fc1ee556b5ba4ab27e Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 15:55:20 +0900 Subject: [PATCH 3/9] Remove unnecessary library dependency --- nirum.cabal | 1 - 1 file changed, 1 deletion(-) diff --git a/nirum.cabal b/nirum.cabal index fd33c43..cda5c86 100644 --- a/nirum.cabal +++ b/nirum.cabal @@ -134,7 +134,6 @@ 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 From 3527e8a20eb4d0d1fc9cf16a4124dc4c590f0619 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 16:05:13 +0900 Subject: [PATCH 4/9] More specific description for options --- src/Nirum/Cli.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Nirum/Cli.hs b/src/Nirum/Cli.hs index d158bef..5bf3182 100644 --- a/src/Nirum/Cli.hs +++ b/src/Nirum/Cli.hs @@ -167,7 +167,8 @@ main = do optsParser = OPT.info (OPT.helper <*> versionOption <*> programOptions) - (OPT.fullDesc <> OPT.progDesc "Nirum compiler." <> + (OPT.fullDesc <> + OPT.progDesc ("Nirum compiler" ++ versionString) <> OPT.header header) header :: String header = "nirum - The IDL compiler and RPC/distributed object framework" @@ -178,10 +179,10 @@ main = do programOptions :: OPT.Parser Opts programOptions = Opts <$> OPT.strOption - (OPT.long "outdir" <> OPT.short 'o' <> OPT.metavar "OUTDIR" <> - OPT.help "out directory") <*> + (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 name") <*> + OPT.help "Target language name") <*> OPT.strArgument - (OPT.metavar "PACKAGE" <> OPT.help "Package directory") + (OPT.metavar "DIR" <> OPT.help "Package directory") From c18968620999d6f55daf27ff5618dead7c8c60ef Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 17:27:48 +0900 Subject: [PATCH 5/9] Import function required only --- src/Nirum/Cli.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nirum/Cli.hs b/src/Nirum/Cli.hs index 5bf3182..d3d4037 100644 --- a/src/Nirum/Cli.hs +++ b/src/Nirum/Cli.hs @@ -8,7 +8,7 @@ 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 Data.Monoid +import Data.Monoid ((<>)) import qualified Options.Applicative as OPT import System.Directory (createDirectoryIfMissing) import System.Exit (die) From 84888b091de958709b14c2a23dbf2d816f4708f8 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 17:34:40 +0900 Subject: [PATCH 6/9] Update README about CLI --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 82f3e6b..542e555 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 compiler0.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 -------- From 54330b446c6a9df09d1014038fbf68c25bdf1d74 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 17:42:18 +0900 Subject: [PATCH 7/9] Re-format help message --- README.md | 4 ++-- src/Nirum/Cli.hs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 542e555..3b028ba 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,10 @@ In order to compile a Nirum package (`examples/`) to a Python package: For more infomration, use `--help` option: $ nirum --help - nirum - The IDL compiler and RPC/distributed object framework + Nirum: The IDL compiler and RPC/distributed object framework Usage: nirum [-v|--version] (-o|--output-dir DIR) (-t|--target TARGET) DIR - Nirum compiler0.3.0 + Nirum compiler 0.3.0 Available options: -h,--help Show this help text diff --git a/src/Nirum/Cli.hs b/src/Nirum/Cli.hs index d3d4037..c4dacdf 100644 --- a/src/Nirum/Cli.hs +++ b/src/Nirum/Cli.hs @@ -168,10 +168,10 @@ main = do OPT.info (OPT.helper <*> versionOption <*> programOptions) (OPT.fullDesc <> - OPT.progDesc ("Nirum compiler" ++ versionString) <> + OPT.progDesc ("Nirum compiler " ++ versionString) <> OPT.header header) header :: String - header = "nirum - The IDL compiler and RPC/distributed object framework" + header = "Nirum: The IDL compiler and RPC/distributed object framework" versionOption :: OPT.Parser (Opts -> Opts) versionOption = OPT.infoOption versionString (OPT.long "version" <> From a27464112c1b4a610ee00960447f6d2965c7abcc Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 21:17:27 +0900 Subject: [PATCH 8/9] Remove DeriveDataTypeable as suggested by lint --- src/Nirum/Cli.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nirum/Cli.hs b/src/Nirum/Cli.hs index c4dacdf..4f27b45 100644 --- a/src/Nirum/Cli.hs +++ b/src/Nirum/Cli.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE ExtendedDefaultRules, QuasiQuotes, DeriveDataTypeable #-} +{-# LANGUAGE ExtendedDefaultRules, QuasiQuotes #-} module Nirum.Cli (main, writeFiles) where import Control.Monad (forM_) From f04fa63e8896133d6d1ad72258660b8ba8c1bd83 Mon Sep 17 00:00:00 2001 From: Hyojun Kang Date: Sat, 11 Mar 2017 22:19:45 +0900 Subject: [PATCH 9/9] Give -t option in appveyor --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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