Skip to content

Commit

Permalink
Use optparse-applicative instead of cmdargs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyojun Kang committed Mar 11, 2017
1 parent be1dbe3 commit 6412618
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
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
, 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
import qualified Options.Applicative as OPT
import System.Directory (createDirectoryIfMissing)
import System.FilePath (takeDirectory, (</>))
import Text.InterpolatedString.Perl6 (qq)
Expand Down Expand Up @@ -56,11 +43,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 @@ -127,24 +109,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 @@ -180,5 +149,38 @@ writeFiles outDir files =
putStrLn outPath
B.writeFile outPath code

data Opts = Opts { outDirectory :: !String
, targetOption :: !String
, packageDirectory :: !String
}

main :: IO ()
main = catchIOError main' $ putStrLn . 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")

0 comments on commit 6412618

Please sign in to comment.