fix:repl: make the run command work in the REPL again

The now-required -- argument was being lost during REPL parsing.
I think this is right, though it's hard to understand.
This commit is contained in:
Simon Michael 2025-03-06 13:03:57 -10:00
parent fd906bec95
commit 9ecc3a56d0
3 changed files with 11 additions and 10 deletions

View File

@ -235,19 +235,11 @@ main = withGhcDebug' $ do
-- cmdname = the full unabbreviated command name, or ""
-- confcmdargs = arguments for the subcommand, from config file
let
-- | cmdargs eats the first double-dash (--) argument, causing problems for
-- the run command. To work around it, we'll insert another.
-- This doesn't break anything that we know of.
doubleDoubleDash args
| "--" `elem` args = let (as,bs) = break (=="--") args in as <> ["--"] <> bs
| otherwise = args
-- Do some argument preprocessing to help cmdargs
cliargs <- getArgs
>>= expandArgsAt -- interpolate @ARGFILEs
<&> replaceNumericFlags -- convert -NUM to --depth=NUM
<&> doubleDoubleDash -- repeat the -- arg as a cmdargs workaround
<&> argsAddDoubleDash -- repeat the first -- arg, as a cmdargs workaround
let
(clicmdarg, cliargswithoutcmd, cliargswithcmdfirst) = moveFlagsAfterCommand cliargs
cliargswithcmdfirstwithoutclispecific = dropCliSpecificOpts cliargswithcmdfirst

View File

@ -58,6 +58,7 @@ module Hledger.Cli.CliOptions (
getHledgerCliOpts',
rawOptsToCliOpts,
cliOptsDropArgs,
argsAddDoubleDash,
outputFormats,
defaultOutputFormat,
CommandHelpStr,
@ -632,6 +633,14 @@ rawOptsToCliOpts rawopts = do
cliOptsDropArgs :: CliOpts -> CliOpts
cliOptsDropArgs copts@CliOpts{rawopts_} = copts{rawopts_ = dropRawOpt "args" rawopts_}
-- | cmdargs eats the first double-dash (--) argument when parsing a command line,
-- which causes problems for the run and repl commands.
-- Sometimes we work around this by duplicating that first -- argument.
-- This doesn't break anything that we know of yet.
argsAddDoubleDash args'
| "--" `elem` args' = let (as,bs) = break (=="--") args' in as <> ["--"] <> bs
| otherwise = args'
-- | A helper for addon commands: this parses options and arguments from
-- the current command line using the given hledger-style cmdargs mode,
-- and returns a CliOpts. Or, with --help or -h present, it prints

View File

@ -178,7 +178,7 @@ runREPL defaultJournalOverride findBuiltinCommand = do
Just "quit" -> return ()
Just "exit" -> return ()
Just input -> do
liftIO $ (runCommand defaultJournalOverride findBuiltinCommand $ parseCommand input)
liftIO $ (runCommand defaultJournalOverride findBuiltinCommand $ argsAddDoubleDash $ parseCommand input)
`catches`
[Handler (\(e::ErrorCall) -> putStr $ show e)
,Handler (\(_::ExitCode) -> return ())