From 9ecc3a56d0ca1c1128fe43c24d3221bf9ae7b02e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 6 Mar 2025 13:03:57 -1000 Subject: [PATCH] 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. --- hledger/Hledger/Cli.hs | 10 +--------- hledger/Hledger/Cli/CliOptions.hs | 9 +++++++++ hledger/Hledger/Cli/Commands/Run.hs | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index bc39d3d8e..967e50bd3 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -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 diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 64af3be4a..5799ed327 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -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 diff --git a/hledger/Hledger/Cli/Commands/Run.hs b/hledger/Hledger/Cli/Commands/Run.hs index bb8276bef..c29a7ef93 100644 --- a/hledger/Hledger/Cli/Commands/Run.hs +++ b/hledger/Hledger/Cli/Commands/Run.hs @@ -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 ())