-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changes from 2 commits
85a5dc8
d1ab100
22cfc46
3527e8a
c189686
84888b0
54330b4
a274641
f04fa63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 모듈에서 어떤 거 가져다 쓰는 거예요? |
||
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." <> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" <> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 metavar은 |
||
OPT.help "out directory") <*> | ||
OPT.strOption | ||
(OPT.long "target" <> OPT.short 't' <> OPT.metavar "TARGET" <> | ||
OPT.help "Target name") <*> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 설명은 “target language name” 정도로 적어주세요. |
||
OPT.strArgument | ||
(OPT.metavar "PACKAGE" <> OPT.help "Package directory") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마찬가지로 metavar은 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test-suite spec
쪽은library
쪽 의존성 버전을 따라가면 되니까, 버전 명시 안해도 될 것 같습니다.