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 2 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
2 changes: 2 additions & 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 Expand Up @@ -133,6 +134,7 @@ test-suite spec
, megaparsec
, mtl
, nirum
, optparse-applicative >=0.13.1 && <0.14
Copy link
Member

Choose a reason for hiding this comment

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

test-suite spec 쪽은 library 쪽 의존성 버전을 따라가면 되니까, 버전 명시 안해도 될 것 같습니다.

, parsec
-- only for dealing with htoml's ParserError
, process >=1.1 && <2
Expand Down
74 changes: 38 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
Copy link
Member

Choose a reason for hiding this comment

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

이 모듈에서 어떤 거 가져다 쓰는 거예요? import qualified로 하거나 필요한 것만 명시적으로 가져오는 게 좋을 듯하네요.

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,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." <>
Copy link
Member

Choose a reason for hiding this comment

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

원래는 버전도 같이 표시했었는데 빠졌네요. 마침표는 없어도 될 것 같습니다.

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

Choose a reason for hiding this comment

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

이거 metavar은 DIR이면 될 것 같습니다. Long option name은 output-dir 정도로 푸는 게 나아 보입니다.

OPT.help "out directory") <*>
OPT.strOption
(OPT.long "target" <> OPT.short 't' <> OPT.metavar "TARGET" <>
OPT.help "Target name") <*>
Copy link
Member

Choose a reason for hiding this comment

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

설명은 “target language name” 정도로 적어주세요.

OPT.strArgument
(OPT.metavar "PACKAGE" <> OPT.help "Package directory")
Copy link
Member

Choose a reason for hiding this comment

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

마찬가지로 metavar은 DIR 정도가 적당할 것 같습니다.

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