Skip to content

Commit 44779ba

Browse files
committed
Rework parsing of database flag: make it properly optional
1 parent 9493521 commit 44779ba

File tree

1 file changed

+30
-49
lines changed

1 file changed

+30
-49
lines changed

src/Action/CmdLine.hs

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE RecordWildCards #-}
22
{-# LANGUAGE DuplicateRecordFields #-}
3-
{-# LANGUAGE OverloadedRecordDot #-}
43
{-# LANGUAGE ApplicativeDo #-}
54

65
module Action.CmdLine(
@@ -139,49 +138,31 @@ defaultDatabaseLang = do
139138
pure legacyLocation
140139
pure $ dir </> "default-haskell-" ++ showVersion (trimVersion 3 version) ++ ".hoo"
141140

142-
-- N.B. This is rather awkward but seems to be the pragmatic way to migrate
143-
-- away from cmdargs without changing the user-visible command-line syntax.
144-
fillInDatabase :: FilePath -> Mode -> Mode
145-
fillInDatabase defDb (Search opts)
146-
| "" <- opts.database = Search $ opts { database = defDb }
147-
fillInDatabase defDb (Generate opts)
148-
| "" <- opts.database = Generate $ opts { database = defDb }
149-
fillInDatabase defDb (Server opts)
150-
| "" <- opts.database = Server $ opts { database = defDb }
151-
fillInDatabase defDb (Replay opts)
152-
| "" <- opts.database = Replay $ opts { database = defDb }
153-
fillInDatabase defDb (Test opts)
154-
| "" <- opts.database = Test $ opts { database = defDb }
155-
fillInDatabase _ mode = mode
156-
157141
getCmdLine :: [String] -> IO (Verbosity, Mode)
158142
getCmdLine args = do
159-
(verbosity, mode) <- execParser cmdline
160-
161-
-- fill in the default database TODO
162-
--args <- if args.database /= "" then pure args else do
163143
defDb <- defaultDatabaseLang
164-
pure (verbosity, fillInDatabase defDb mode)
144+
(verbosity, mode) <- execParser (cmdline defDb)
145+
pure (verbosity, mode)
165146

166-
cmdline :: ParserInfo (Verbosity, Mode)
167-
cmdline =
147+
cmdline :: FilePath -> ParserInfo (Verbosity, Mode)
148+
cmdline defDb =
168149
O.info ((,) <$> verbosity <*> mode' <**> helper <**> simpleVersioner (showVersion version)) (header name)
169150
where
170-
mode' = mode <|> fmap Search searchOpts
151+
mode' = mode defDb <|> fmap Search (searchOpts defDb)
171152
verbosity = flag VerbosityNormal VerbosityLoud (short 'v' <> long "verbose" <> help "emit verbose output")
172153
name = "Hoogle " ++ showVersion version ++ ", https://hoogle.haskell.org/"
173154

174-
mode :: Parser Mode
175-
mode = hsubparser
176-
$ command "search" (O.info (Search <$> searchOpts) (progDesc "Perform a search"))
177-
<> command "generate" (O.info (Generate <$> generateOpts) (progDesc "Generate Hoogle databases"))
178-
<> command "serve" (O.info (Server <$> serverOpts) (progDesc "Start a Hoogle server"))
179-
<> command "replay" (O.info (Replay <$> replayOpts) (progDesc "Replay a log file"))
180-
<> command "test" (O.info (Test <$> testOpts) (progDesc "Run the test suite"))
155+
mode :: FilePath -> Parser Mode
156+
mode defDb = hsubparser
157+
$ command "search" (O.info (Search <$> searchOpts defDb) (progDesc "Perform a search"))
158+
<> command "generate" (O.info (Generate <$> generateOpts defDb) (progDesc "Generate Hoogle databases"))
159+
<> command "serve" (O.info (Server <$> serverOpts defDb) (progDesc "Start a Hoogle server"))
160+
<> command "replay" (O.info (Replay <$> replayOpts defDb) (progDesc "Replay a log file"))
161+
<> command "test" (O.info (Test <$> testOpts defDb) (progDesc "Run the test suite"))
181162

182-
databaseFlag :: Parser FilePath
183-
databaseFlag =
184-
option str (long "database" <> short 'd' <> metavar "FILE" <> help "Name of database to use (use .hoo extension)")
163+
databaseFlag :: FilePath -> Parser FilePath
164+
databaseFlag defDb =
165+
option str (long "database" <> short 'd' <> metavar "FILE" <> help "Name of database to use (use .hoo extension)" <> value defDb <> showDefault)
185166

186167
logsFlag :: Parser FilePath
187168
logsFlag =
@@ -195,25 +176,25 @@ scopeFlag :: Parser String
195176
scopeFlag =
196177
option str (long "scope" <> short 's' <> help "Default scope to start with")
197178

198-
searchOpts :: Parser SearchOpts
199-
searchOpts = do
179+
searchOpts :: FilePath -> Parser SearchOpts
180+
searchOpts defDb = do
200181
color <- optional $ switch (long "colour" <> help "Use colored output (requires ANSI terminal)")
201182
json <- switch (long "json" <> help "Get result as JSON")
202183
jsonl <- switch (long "jsonl" <> help "Get result as JSONL (JSON Lines)")
203184
link <- switch (long "link" <> help "Give URL's for each result")
204185
numbers <- switch (long "numbers" <> help "Give counter for each result")
205186
info <- switch (long "info" <> help "Give extended information about the first n results (set n with --count, default is 1)")
206-
database <- databaseFlag
187+
database <- databaseFlag defDb
207188
count <- optional $ option auto (short 'n' <> long "count" <> help "Maximum number of results to return (defaults to 10)")
208189
query <- some $ argument str (metavar "QUERY")
209190
repeat_ <- repeatFlag
210191
compare_ <- many $ option str (long "compare" <> metavar "SIG" <> help "Type signatures to compare against")
211192
pure $ SearchOpts {..}
212193

213-
generateOpts :: Parser GenerateOpts
214-
generateOpts = do
194+
generateOpts :: FilePath -> Parser GenerateOpts
195+
generateOpts defDb = do
215196
download <- optional $ switch (long "download" <> help "Download all files from the web")
216-
database <- databaseFlag
197+
database <- databaseFlag defDb
217198
insecure <- switch (long "insecure" <> short 'i' <> help "Allow insecure HTTPS connections")
218199
include <- many $ argument str (metavar "PACKAGE" <> help "Packages to include")
219200
local_ <- many $ option (fromMaybe "" <$> optional str) (long "local" <> short 'l' <> help "Index local packages and link to local haddock docs")
@@ -233,10 +214,10 @@ tcpEndpoint =
233214
host = option str (long "host" <> value "*" <> help "Set the host to bind on (e.g., an ip address; '!4' for ipv4-only; '!6' for ipv6-only; default: '*' for any host).")
234215
port = option auto (long "port" <> short 'p' <> value 8080 <> metavar "PORT" <> help "Port number")
235216

236-
serverOpts :: Parser ServerOpts
237-
serverOpts = do
217+
serverOpts :: FilePath -> Parser ServerOpts
218+
serverOpts defDb = do
238219
endpoint <- unixEndpoint <|> tcpEndpoint
239-
database <- databaseFlag
220+
database <- databaseFlag defDb
240221
cdn <- option str (value "" <> metavar "URL" <> help "URL prefix to use")
241222
logs <- logsFlag
242223
local <- switch (long "local" <> help "Allow following file:// links, restricts to 127.0.0.1 Set --host explicitely (including to '*' for any host) to override the localhost-only behaviour")
@@ -251,17 +232,17 @@ serverOpts = do
251232
no_security_headers <- switch (long "no-security-headers" <> short 'n' <> help "Don't send CSP security headers")
252233
pure ServerOpts {..}
253234

254-
replayOpts :: Parser ReplayOpts
255-
replayOpts = do
235+
replayOpts :: FilePath -> Parser ReplayOpts
236+
replayOpts defDb = do
256237
logs <- logsFlag
257-
database <- databaseFlag
238+
database <- databaseFlag defDb
258239
repeat_ <- repeatFlag
259240
scope <- scopeFlag
260241
pure ReplayOpts {..}
261242

262-
testOpts :: Parser TestOpts
263-
testOpts = do
243+
testOpts :: FilePath -> Parser TestOpts
244+
testOpts defDb = do
264245
deep <- switch (long "deep" <> help "Run extra long tests")
265-
database <- databaseFlag
246+
database <- databaseFlag defDb
266247
disable_network_tests <- switch (long "disable-network-tests" <> help "Disables the use of network tests")
267248
pure TestOpts {..}

0 commit comments

Comments
 (0)