fix: make no-argument --debug flag work with ui & web also

This commit is contained in:
Simon Michael 2022-10-31 18:20:35 -10:00
parent 0bb83b5620
commit c25c5cef44
4 changed files with 12 additions and 11 deletions

View File

@ -101,7 +101,7 @@ getHledgerUIOpts :: IO UIOpts
--getHledgerUIOpts = processArgs uimode >>= return >>= rawOptsToUIOpts
getHledgerUIOpts = do
args <- getArgs >>= expandArgsAt
let args' = replaceNumericFlags args
let args' = replaceNumericFlags $ ensureDebugHasArg args
let cmdargopts = either usageError id $ process uimode args'
rawOptsToUIOpts cmdargopts

View File

@ -186,7 +186,7 @@ checkWebOpts = id
getHledgerWebOpts :: IO WebOpts
getHledgerWebOpts = do
args <- fmap replaceNumericFlags . expandArgsAt =<< getArgs
args <- fmap (replaceNumericFlags . ensureDebugHasArg) . expandArgsAt =<< getArgs
rawOptsToWebOpts . either usageError id $ process webmode args
data Capability

View File

@ -37,6 +37,7 @@ module Hledger.Cli.CliOptions (
withAliases,
likelyExecutablesInPath,
hledgerExecutablesInPath,
ensureDebugHasArg,
-- * CLI options
CliOpts(..),
@ -798,3 +799,10 @@ instance HasReportOptsNoUpdate CliOpts where
instance HasReportOpts CliOpts where
reportOpts = reportSpec.reportOpts
-- | Convert an argument-less --debug flag to --debug=1 in the given arguments list.
-- Used by hledger/ui/web to make their command line parsing easier somehow.
ensureDebugHasArg as = case break (=="--debug") as of
(bs,"--debug":c:cs) | null c || not (all isDigit c) -> bs++"--debug=1":c:cs
(bs,["--debug"]) -> bs++["--debug=1"]
_ -> as

View File

@ -40,7 +40,6 @@ etc.
module Hledger.Cli.Main where
import Data.Char (isDigit)
import Data.List
import Safe
import qualified System.Console.CmdArgs.Explicit as C
@ -220,23 +219,17 @@ argsToCliOpts args addons = do
-- | A hacky workaround for cmdargs not accepting flags before the
-- subcommand name: try to detect and move such flags after the
-- command. This allows the user to put them in either position.
-- The order of options is not preserved, but this should be ok.
-- The order of options is not preserved, but that should be ok.
--
-- Since we're not parsing flags as precisely as cmdargs here, this is
-- imperfect. We make a decent effort to:
-- - move all no-argument help/input/report flags
-- - move all required-argument help/input/report flags along with their values, space-separated or not
-- - ensure --debug has an argument (because.. "or this all goes to hell")
-- - not confuse things further or cause misleading errors.
moveFlagsAfterCommand :: [String] -> [String]
moveFlagsAfterCommand args = moveArgs $ ensureDebugHasArg args
where
-- quickly! make sure --debug has a numeric argument, or this all goes to hell
ensureDebugHasArg as =
case break (=="--debug") as of
(bs,"--debug":c:cs) | null c || not (all isDigit c) -> bs++"--debug=1":c:cs
(bs,["--debug"]) -> bs++["--debug=1"]
_ -> as
moveArgs args1 = insertFlagsAfterCommand $ moveArgs' (args1, [])
where
-- -f FILE ..., --alias ALIAS ...