@@ -28,7 +28,7 @@ import Text.Megaparsec (ParseErrorBundle, Parsec, errorBundlePretty, runParser)
28
28
29
29
data Opts = Opts Command String
30
30
31
- newtype ValidateOpts = ValidateOpts { noPrelude :: Bool }
31
+ newtype ValidateOpts = ValidateOpts { vNoPrelude :: Bool }
32
32
33
33
data Command
34
34
= Format FormatOpts
@@ -53,6 +53,7 @@ pCBOROutputFormat = eitherReader $ \case
53
53
data GenOpts = GenOpts
54
54
{ itemName :: T. Text
55
55
, outputFormat :: CBOROutputFormat
56
+ , gNoPrelude :: Bool
56
57
}
57
58
58
59
pGenOpts :: Parser GenOpts
@@ -71,6 +72,10 @@ pGenOpts =
71
72
<> help " Output format"
72
73
<> value AsCBOR
73
74
)
75
+ <*> switch
76
+ ( long " no-prelude"
77
+ <> help " Do not include the CDDL prelude."
78
+ )
74
79
75
80
newtype FormatOpts = FormatOpts
76
81
{ sort :: Bool }
@@ -98,19 +103,19 @@ opts =
98
103
( command
99
104
" format"
100
105
( info
101
- (Format <$> pFormatOpts)
106
+ (Format <$> pFormatOpts <**> helper )
102
107
(progDesc " Format the provided CDDL file" )
103
108
)
104
109
<> command
105
110
" validate"
106
111
( info
107
- (Validate <$> pValidateOpts)
112
+ (Validate <$> pValidateOpts <**> helper )
108
113
(progDesc " Validate the provided CDDL file" )
109
114
)
110
115
<> command
111
116
" gen"
112
117
( info
113
- (GenerateCBOR <$> pGenOpts)
118
+ (GenerateCBOR <$> pGenOpts <**> helper )
114
119
(progDesc " Generate a CBOR term matching the schema" )
115
120
)
116
121
)
@@ -134,29 +139,40 @@ run (Opts cmd cddlFile) = do
134
139
Left err -> do
135
140
putStrLnErr $ errorBundlePretty err
136
141
exitFailure
137
- Right res -> case cmd of
138
- Format fOpts ->
139
- let defs = if sort fOpts then sortCDDL res else res
140
- in putDocW 80 $ pretty defs
141
- Validate vOpts ->
142
- let
143
- resWithPrelude
144
- | noPrelude vOpts = res
145
- | otherwise = prependPrelude res
146
- in
147
- case fullResolveCDDL resWithPrelude of
148
- Left err -> putStrLnErr (show err) >> exitFailure
149
- Right _ -> exitSuccess
150
- (GenerateCBOR x) -> case fullResolveCDDL res of
151
- Left err -> putStrLnErr (show err) >> exitFailure
152
- Right mt -> do
153
- stdGen <- getStdGen
154
- let term = generateCBORTerm mt (Name $ itemName x) stdGen
155
- in case outputFormat x of
156
- AsTerm -> print term
157
- AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
158
- AsCBOR -> BSC. putStrLn . Base16. encode . toStrictByteString $ encodeTerm term
159
- AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
142
+ Right res ->
143
+ case cmd of
144
+ Format fOpts ->
145
+ let
146
+ defs
147
+ | sort fOpts = sortCDDL res
148
+ | otherwise = res
149
+ in
150
+ putDocW 80 $ pretty defs
151
+ Validate vOpts ->
152
+ let
153
+ res'
154
+ | vNoPrelude vOpts = res
155
+ | otherwise = prependPrelude res
156
+ in
157
+ case fullResolveCDDL res' of
158
+ Left err -> putStrLnErr (show err) >> exitFailure
159
+ Right _ -> exitSuccess
160
+ (GenerateCBOR gOpts) ->
161
+ let
162
+ res'
163
+ | gNoPrelude gOpts = res
164
+ | otherwise = prependPrelude res
165
+ in
166
+ case fullResolveCDDL res' of
167
+ Left err -> putStrLnErr (show err) >> exitFailure
168
+ Right mt -> do
169
+ stdGen <- getStdGen
170
+ let term = generateCBORTerm mt (Name $ itemName gOpts) stdGen
171
+ in case outputFormat gOpts of
172
+ AsTerm -> print term
173
+ AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
174
+ AsCBOR -> BSC. putStrLn . Base16. encode . toStrictByteString $ encodeTerm term
175
+ AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
160
176
161
177
putStrLnErr :: String -> IO ()
162
178
putStrLnErr = hPutStrLn stderr
0 commit comments