From a925e73b5396b97167e00a925f77243744aa535a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 19 Oct 2024 16:25:20 -1000 Subject: [PATCH] imp:cli: handle -NUM in config file properly --- hledger.conf | 5 ++++- hledger/Hledger/Cli.hs | 27 ++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/hledger.conf b/hledger.conf index d6afe111d..dbd433dab 100644 --- a/hledger.conf +++ b/hledger.conf @@ -2,11 +2,14 @@ # while developing and testing hledger in this directory. # For an example config, see hledger.conf.sample. +# -2 +# this works (equivalent to --depth 2) + # --pager n # this works # --color n -# this mostly works, but does not affect +# this mostly works, except it does not affect # 1. debug output # 2. the ansi helpers used by check recentassertions diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index 77ac7ccac..ee1a3a1b4 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -255,7 +255,7 @@ main = withGhcDebug' $ do -- or if there is none, from the first non-flag argument on the command line. let - confallgenargs = confLookup "general" conf + confallgenargs = confLookup "general" conf & replaceNumericFlags -- we don't try to move flags/values preceding a command argument here; -- if a command name is written in the config file, it must be first (confcmdarg, confothergenargs) = case confallgenargs of @@ -264,20 +264,21 @@ main = withGhcDebug' $ do cmdarg = if not $ null confcmdarg then confcmdarg else clicmdarg nocmdprovided = null cmdarg - -- The argument may be an abbreviated command name. + -- The argument may be an abbreviated command name, which we need to expand. + -- Run cmdargs on conf + cli args to get the full command name. -- If no command argument was provided, or if cmdargs fails because -- the command line contains a bad flag or wrongly present/missing flag value, -- cmdname will be "". args = [confcmdarg | not $ null confcmdarg] <> cliargswithcmdfirstwithoutclispecific - -- XXX Unknown flag: --depth while parsing these args for command name cmdname = stringopt "command" $ cmdargsParse "for command name" (mainmode addons) args + badcmdprovided = null cmdname && not nocmdprovided isaddoncmd = not (null cmdname) && cmdname `elem` addons - -- And get the builtin command's action, if any. + -- And get the builtin command's mode and action, if any. mbuiltincmdaction = findBuiltinCommand cmdname - effectivemode = maybe (mainmode []) fst mbuiltincmdaction + effectivemode = maybe (mainmode []) fst mbuiltincmdaction when (isJust mconffile) $ do unless (null confcmdarg) $ @@ -301,8 +302,11 @@ main = withGhcDebug' $ do | otherwise = dropUnsupportedOpts effectivemode confothergenargs excludedgenargsfromconf = confothergenargs \\ supportedgenargsfromconf confcmdargs - | null cmdname = [] - | otherwise = confLookup cmdname conf & if isaddoncmd then ("--":) else id + | null cmdname = [] + | otherwise = + confLookup cmdname conf + & replaceNumericFlags + & if isaddoncmd then ("--":) else id when (isJust mconffile) $ do dbgIO1 "using general args from config file" confothergenargs @@ -315,8 +319,13 @@ main = withGhcDebug' $ do let finalargs = - [cmdarg | not $ null cmdarg] <> supportedgenargsfromconf <> confcmdargs <> [clicmdarg | not $ null confcmdarg] <> cliargswithoutcmd + [cmdarg | not $ null cmdarg] + <> supportedgenargsfromconf + <> confcmdargs + <> [clicmdarg | not $ null confcmdarg] + <> cliargswithoutcmd & replaceNumericFlags -- convert any -NUM opts from the config file + -- finalargs' <- expandArgsAt finalargs -- expand @ARGFILEs in the config file ? don't bother dbgIO1 "final args" finalargs @@ -451,7 +460,7 @@ argsToCliOpts args addons = do cmdargsParse :: String -> Mode RawOpts -> [String] -> RawOpts cmdargsParse desc m args0 = process m (ensureDebugFlagHasVal args0) & either - (\e -> error' $ e <> " while parsing these args " <> desc <> ": " <> unwords (map quoteIfNeeded args0)) + (\e -> error' $ e <> "\n* while parsing the following args, " <> desc <> ":\n* " <> unwords (map quoteIfNeeded args0)) (traceOrLogAt verboseDebugLevel ("cmdargs: parsing " <> desc <> ": " <> show args0)) -- XXX better error message when cmdargs fails (eg spaced/quoted/malformed flag values) ?