Skip to content

Commit 4c8f997

Browse files
authored
dhall-bash: Removed optparse-generic dependency (#2648)
1 parent fb56888 commit 4c8f997

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

dhall-bash/dhall-bash.cabal

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ Executable dhall-to-bash
4444
Other-Modules:
4545
Paths_dhall_bash
4646
Build-Depends:
47-
base >= 4.11.0.0 && < 5 ,
48-
bytestring ,
49-
dhall ,
50-
dhall-bash ,
51-
optparse-generic >= 1.1.1 && < 1.6,
47+
base >= 4.11.0.0 && < 5 ,
48+
bytestring ,
49+
dhall ,
50+
dhall-bash ,
51+
optparse-applicative >= 0.14.0.0 && < 0.19,
5252
text
5353
GHC-Options: -Wall
5454
Default-Language: Haskell2010

dhall-bash/exec/Main.hs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE ApplicativeDo #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE DeriveAnyClass #-}
34
{-# LANGUAGE DeriveGeneric #-}
@@ -8,8 +9,10 @@
89
module Main where
910

1011
import Control.Exception (SomeException)
11-
import Data.ByteString (ByteString)
12-
import Options.Generic (Generic, ParseRecord, type (<?>) (..))
12+
import Data.Text (Text)
13+
import Data.Text.Encoding (encodeUtf8)
14+
import GHC.Generics (Generic)
15+
import Options.Applicative
1316
import System.Exit (ExitCode (..))
1417

1518
import qualified Control.Exception
@@ -22,32 +25,57 @@ import qualified Dhall.Import
2225
import qualified Dhall.Parser
2326
import qualified Dhall.TypeCheck
2427
import qualified GHC.IO.Encoding
25-
import qualified Options.Generic
2628
import qualified Paths_dhall_bash
2729
import qualified System.Exit
2830
import qualified System.IO
2931

3032
data Options = Options
3133
{ explain :: Bool
32-
<?> "Explain error messages in detail"
33-
, declare :: Maybe ByteString
34-
<?> "Declare the given variable as a statement instead of an expression"
34+
, declare :: Maybe Text
3535
, version :: Bool
36-
<?> "Display version"
37-
} deriving (Generic, ParseRecord)
36+
} deriving (Generic)
37+
38+
-- | Parser for all the command arguments and options
39+
parseOptions :: Parser Options
40+
parseOptions = do
41+
explain <- switch
42+
( long "explain"
43+
<> help "Explain error messages in detail"
44+
)
45+
46+
declare <- (optional . strOption)
47+
( long "declare"
48+
<> metavar "NAME"
49+
<> help "Declare the given variable as a statement instead of an expression"
50+
)
51+
52+
version <- switch
53+
( long "version"
54+
<> help "Display version"
55+
)
56+
57+
pure Options{..}
58+
59+
parserInfo :: ParserInfo Options
60+
parserInfo =
61+
info
62+
(helper <*> parseOptions)
63+
( fullDesc
64+
<> progDesc "Compile Dhall to Bash"
65+
)
3866

3967
main :: IO ()
4068
main = do
4169
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
42-
Options {..} <- Options.Generic.getRecord "Compile Dhall to Bash"
70+
Options {..} <- execParser parserInfo
4371

44-
if unHelpful version
72+
if version
4573
then do
4674
putStrLn (Data.Version.showVersion Paths_dhall_bash.version)
4775
System.Exit.exitSuccess
4876
else return ()
4977

50-
(if unHelpful explain then Dhall.detailed else id) (handle (do
78+
(if explain then Dhall.detailed else id) (handle (do
5179
inText <- Data.Text.IO.getContents
5280

5381
expr <- case Dhall.Parser.exprFromText "(input)" inText of
@@ -59,13 +87,13 @@ main = do
5987
Left err -> Control.Exception.throwIO err
6088
Right _ -> return ()
6189

62-
bytes <- case unHelpful declare of
90+
bytes <- case declare of
6391
Nothing -> do
6492
case Dhall.Bash.dhallToExpression expr' of
6593
Left err -> Control.Exception.throwIO err
6694
Right bytes -> return bytes
6795
Just var -> do
68-
case Dhall.Bash.dhallToStatement expr' var of
96+
case Dhall.Bash.dhallToStatement expr' (encodeUtf8 var) of
6997
Left err -> Control.Exception.throwIO err
7098
Right bytes -> return bytes
7199
Data.ByteString.putStr bytes ))

0 commit comments

Comments
 (0)