fix: make no-argument --debug flag work with ui & web also
This commit is contained in:
parent
0bb83b5620
commit
c25c5cef44
@ -101,7 +101,7 @@ getHledgerUIOpts :: IO UIOpts
|
|||||||
--getHledgerUIOpts = processArgs uimode >>= return >>= rawOptsToUIOpts
|
--getHledgerUIOpts = processArgs uimode >>= return >>= rawOptsToUIOpts
|
||||||
getHledgerUIOpts = do
|
getHledgerUIOpts = do
|
||||||
args <- getArgs >>= expandArgsAt
|
args <- getArgs >>= expandArgsAt
|
||||||
let args' = replaceNumericFlags args
|
let args' = replaceNumericFlags $ ensureDebugHasArg args
|
||||||
let cmdargopts = either usageError id $ process uimode args'
|
let cmdargopts = either usageError id $ process uimode args'
|
||||||
rawOptsToUIOpts cmdargopts
|
rawOptsToUIOpts cmdargopts
|
||||||
|
|
||||||
|
|||||||
@ -186,7 +186,7 @@ checkWebOpts = id
|
|||||||
|
|
||||||
getHledgerWebOpts :: IO WebOpts
|
getHledgerWebOpts :: IO WebOpts
|
||||||
getHledgerWebOpts = do
|
getHledgerWebOpts = do
|
||||||
args <- fmap replaceNumericFlags . expandArgsAt =<< getArgs
|
args <- fmap (replaceNumericFlags . ensureDebugHasArg) . expandArgsAt =<< getArgs
|
||||||
rawOptsToWebOpts . either usageError id $ process webmode args
|
rawOptsToWebOpts . either usageError id $ process webmode args
|
||||||
|
|
||||||
data Capability
|
data Capability
|
||||||
|
|||||||
@ -37,6 +37,7 @@ module Hledger.Cli.CliOptions (
|
|||||||
withAliases,
|
withAliases,
|
||||||
likelyExecutablesInPath,
|
likelyExecutablesInPath,
|
||||||
hledgerExecutablesInPath,
|
hledgerExecutablesInPath,
|
||||||
|
ensureDebugHasArg,
|
||||||
|
|
||||||
-- * CLI options
|
-- * CLI options
|
||||||
CliOpts(..),
|
CliOpts(..),
|
||||||
@ -798,3 +799,10 @@ instance HasReportOptsNoUpdate CliOpts where
|
|||||||
|
|
||||||
instance HasReportOpts CliOpts where
|
instance HasReportOpts CliOpts where
|
||||||
reportOpts = reportSpec.reportOpts
|
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
|
||||||
|
|||||||
@ -40,7 +40,6 @@ etc.
|
|||||||
|
|
||||||
module Hledger.Cli.Main where
|
module Hledger.Cli.Main where
|
||||||
|
|
||||||
import Data.Char (isDigit)
|
|
||||||
import Data.List
|
import Data.List
|
||||||
import Safe
|
import Safe
|
||||||
import qualified System.Console.CmdArgs.Explicit as C
|
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
|
-- | A hacky workaround for cmdargs not accepting flags before the
|
||||||
-- subcommand name: try to detect and move such flags after the
|
-- subcommand name: try to detect and move such flags after the
|
||||||
-- command. This allows the user to put them in either position.
|
-- 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
|
-- Since we're not parsing flags as precisely as cmdargs here, this is
|
||||||
-- imperfect. We make a decent effort to:
|
-- imperfect. We make a decent effort to:
|
||||||
-- - move all no-argument help/input/report flags
|
-- - move all no-argument help/input/report flags
|
||||||
-- - move all required-argument help/input/report flags along with their values, space-separated or not
|
-- - 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.
|
-- - not confuse things further or cause misleading errors.
|
||||||
moveFlagsAfterCommand :: [String] -> [String]
|
moveFlagsAfterCommand :: [String] -> [String]
|
||||||
moveFlagsAfterCommand args = moveArgs $ ensureDebugHasArg args
|
moveFlagsAfterCommand args = moveArgs $ ensureDebugHasArg args
|
||||||
where
|
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, [])
|
moveArgs args1 = insertFlagsAfterCommand $ moveArgs' (args1, [])
|
||||||
where
|
where
|
||||||
-- -f FILE ..., --alias ALIAS ...
|
-- -f FILE ..., --alias ALIAS ...
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user