From c25c5cef4433cc62aef0fdc715eae5abfba51836 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 31 Oct 2022 18:20:35 -1000 Subject: [PATCH] fix: make no-argument --debug flag work with ui & web also --- hledger-ui/Hledger/UI/UIOptions.hs | 2 +- hledger-web/Hledger/Web/WebOptions.hs | 2 +- hledger/Hledger/Cli/CliOptions.hs | 8 ++++++++ hledger/Hledger/Cli/Main.hs | 11 ++--------- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/hledger-ui/Hledger/UI/UIOptions.hs b/hledger-ui/Hledger/UI/UIOptions.hs index 81d049298..53373623d 100644 --- a/hledger-ui/Hledger/UI/UIOptions.hs +++ b/hledger-ui/Hledger/UI/UIOptions.hs @@ -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 diff --git a/hledger-web/Hledger/Web/WebOptions.hs b/hledger-web/Hledger/Web/WebOptions.hs index 0ca73604c..40b3468fa 100644 --- a/hledger-web/Hledger/Web/WebOptions.hs +++ b/hledger-web/Hledger/Web/WebOptions.hs @@ -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 diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 8337c0020..016ab9036 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -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 diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index c8fe5d6ac..8ca2e3c9d 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -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 ...