Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use optparse-applicative instead of cmdargs #111

Merged
merged 9 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“Nirum”은 고유명사니까 대문자로 시작을…


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

버전 앞에 띄어쓰기 넣어주세요


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
--------
Expand Down
1 change: 1 addition & 0 deletions nirum.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 39 additions & 36 deletions src/Nirum/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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, (</>))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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') ->
Expand Down Expand Up @@ -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")
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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