run: support @ARGFILEs (fixes #2330)

This commit is contained in:
Dmitry Astapov 2025-02-23 12:34:36 +00:00 committed by Simon Michael
parent 6569714d37
commit 88a4b0abae
3 changed files with 7 additions and 6 deletions

View File

@ -30,6 +30,7 @@ import Control.Monad (forM_)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Control.Monad.Extra (concatMapM) import Control.Monad.Extra (concatMapM)
import System.Console.CmdArgs.Explicit (expandArgsAt)
import System.Directory (doesFileExist) import System.Directory (doesFileExist)
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import System.Console.Haskeline import System.Console.Haskeline
@ -92,7 +93,7 @@ parseCommand line =
runCommand :: (String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())) -> [String] -> IO () runCommand :: (String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())) -> [String] -> IO ()
runCommand findBuiltinCommand cmdline = do runCommand findBuiltinCommand cmdline = do
dbg1IO "running command" cmdline dbg1IO "runCommand for" cmdline
-- # begins a comment, ignore everything after # -- # begins a comment, ignore everything after #
case cmdline of case cmdline of
"echo":args -> putStrLn $ unwords $ args "echo":args -> putStrLn $ unwords $ args
@ -100,7 +101,11 @@ runCommand findBuiltinCommand cmdline = do
case findBuiltinCommand cmdname of case findBuiltinCommand cmdname of
Nothing -> putStrLn $ unwords (cmdname:args) Nothing -> putStrLn $ unwords (cmdname:args)
Just (cmdmode,cmdaction) -> do Just (cmdmode,cmdaction) -> do
opts <- getHledgerCliOpts' cmdmode args -- Normally expandArgsAt is done by the Cli.hs, but it stops at the first '--', so we need
-- to do it here as well to make sure that each command can use @ARGFILEs
args' <- expandArgsAt args
dbg1IO "runCommand final args" (cmdname,args')
opts <- getHledgerCliOpts' cmdmode args'
withJournalCached opts (cmdaction opts) withJournalCached opts (cmdaction opts)
[] -> return () [] -> return ()

View File

@ -33,8 +33,6 @@ You can use `#!/usr/bin/env hledger run` in the first line of the file to make i
- If you meant to provide file name as an argument, but made a mistake and a gave file name that does not exist, "run" will attempt to interpret it as a command. - If you meant to provide file name as an argument, but made a mistake and a gave file name that does not exist, "run" will attempt to interpret it as a command.
- Numeric flags like `-3` do not work, use long form `--depth 3`
- You can pass `-f` to the `run` itself, and also to any commands given after it (or in the command file, or via REPL). When specific command does not have `-f` in its flags, it will use the journal(s) specified in the arguments of `run`. If command does have `-f` flag, this journal would be read and its contents would be cache, so if several commands specify the same `-f` flag, they will read the journal only once. - You can pass `-f` to the `run` itself, and also to any commands given after it (or in the command file, or via REPL). When specific command does not have `-f` in its flags, it will use the journal(s) specified in the arguments of `run`. If command does have `-f` flag, this journal would be read and its contents would be cache, so if several commands specify the same `-f` flag, they will read the journal only once.
### Examples: ### Examples:

View File

@ -42,8 +42,6 @@ Caveats:
and a gave file name that does not exist, "run" will attempt to and a gave file name that does not exist, "run" will attempt to
interpret it as a command. interpret it as a command.
- Numeric flags like -3 do not work, use long form --depth 3
- You can pass -f to the run itself, and also to any commands given - You can pass -f to the run itself, and also to any commands given
after it (or in the command file, or via REPL). When specific after it (or in the command file, or via REPL). When specific
command does not have -f in its flags, it will use the journal(s) command does not have -f in its flags, it will use the journal(s)