clean up options validation a little
This commit is contained in:
parent
4f351d6c9c
commit
4dd1e9c725
@ -12,6 +12,7 @@ module Hledger.Reports.ReportOptions (
|
|||||||
FormatStr,
|
FormatStr,
|
||||||
defreportopts,
|
defreportopts,
|
||||||
rawOptsToReportOpts,
|
rawOptsToReportOpts,
|
||||||
|
checkReportOpts,
|
||||||
flat_,
|
flat_,
|
||||||
tree_,
|
tree_,
|
||||||
dateSpanFromOpts,
|
dateSpanFromOpts,
|
||||||
@ -125,7 +126,7 @@ defreportopts = ReportOpts
|
|||||||
def
|
def
|
||||||
|
|
||||||
rawOptsToReportOpts :: RawOpts -> IO ReportOpts
|
rawOptsToReportOpts :: RawOpts -> IO ReportOpts
|
||||||
rawOptsToReportOpts rawopts = do
|
rawOptsToReportOpts rawopts = checkReportOpts <$> do
|
||||||
d <- getCurrentDay
|
d <- getCurrentDay
|
||||||
return defreportopts{
|
return defreportopts{
|
||||||
begin_ = maybesmartdateopt d "begin" rawopts
|
begin_ = maybesmartdateopt d "begin" rawopts
|
||||||
@ -146,7 +147,7 @@ rawOptsToReportOpts rawopts = do
|
|||||||
,monthly_ = boolopt "monthly" rawopts
|
,monthly_ = boolopt "monthly" rawopts
|
||||||
,quarterly_ = boolopt "quarterly" rawopts
|
,quarterly_ = boolopt "quarterly" rawopts
|
||||||
,yearly_ = boolopt "yearly" rawopts
|
,yearly_ = boolopt "yearly" rawopts
|
||||||
,format_ = maybestringopt "format" rawopts
|
,format_ = maybestringopt "format" rawopts -- XXX move to CliOpts or move validation from Cli.Options to here
|
||||||
,query_ = unwords $ listofstringopt "args" rawopts -- doesn't handle an arg like "" right
|
,query_ = unwords $ listofstringopt "args" rawopts -- doesn't handle an arg like "" right
|
||||||
,average_ = boolopt "average" rawopts
|
,average_ = boolopt "average" rawopts
|
||||||
,related_ = boolopt "related" rawopts
|
,related_ = boolopt "related" rawopts
|
||||||
@ -158,6 +159,11 @@ rawOptsToReportOpts rawopts = do
|
|||||||
,value_ = boolopt "value" rawopts
|
,value_ = boolopt "value" rawopts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- | Do extra validation of opts, raising an error if there is trouble.
|
||||||
|
checkReportOpts :: ReportOpts -> ReportOpts
|
||||||
|
checkReportOpts ropts@ReportOpts{..} =
|
||||||
|
ropts
|
||||||
|
|
||||||
accountlistmodeopt :: RawOpts -> AccountListMode
|
accountlistmodeopt :: RawOpts -> AccountListMode
|
||||||
accountlistmodeopt rawopts =
|
accountlistmodeopt rawopts =
|
||||||
case reverse $ filter (`elem` ["tree","flat"]) $ map fst rawopts of
|
case reverse $ filter (`elem` ["tree","flat"]) $ map fst rawopts of
|
||||||
|
|||||||
@ -59,23 +59,21 @@ defuiopts = UIOpts
|
|||||||
|
|
||||||
-- instance Default CliOpts where def = defcliopts
|
-- instance Default CliOpts where def = defcliopts
|
||||||
|
|
||||||
toUIOpts :: RawOpts -> IO UIOpts
|
rawOptsToUIOpts :: RawOpts -> IO UIOpts
|
||||||
toUIOpts rawopts = do
|
rawOptsToUIOpts rawopts = checkUIOpts <$> do
|
||||||
cliopts <- rawOptsToCliOpts rawopts
|
cliopts <- rawOptsToCliOpts rawopts
|
||||||
return defuiopts {
|
return defuiopts {
|
||||||
debug_ui_ = boolopt "debug-ui" rawopts
|
debug_ui_ = boolopt "debug-ui" rawopts
|
||||||
,cliopts_ = cliopts
|
,cliopts_ = cliopts
|
||||||
}
|
}
|
||||||
|
|
||||||
checkUIOpts :: UIOpts -> IO UIOpts
|
checkUIOpts :: UIOpts -> UIOpts
|
||||||
checkUIOpts opts = do
|
checkUIOpts opts =
|
||||||
checkCliOpts $ cliopts_ opts
|
either optserror (const opts) $ do
|
||||||
case maybestringopt "theme" $ rawopts_ $ cliopts_ opts of
|
case maybestringopt "theme" $ rawopts_ $ cliopts_ opts of
|
||||||
Just t | not $ elem t themeNames ->
|
Just t | not $ elem t themeNames -> Left $ "invalid theme name: "++t
|
||||||
optserror $ "invalid theme name: "++t
|
_ -> Right ()
|
||||||
_ -> return ()
|
|
||||||
return opts
|
|
||||||
|
|
||||||
getHledgerUIOpts :: IO UIOpts
|
getHledgerUIOpts :: IO UIOpts
|
||||||
getHledgerUIOpts = processArgs uimode >>= return . decodeRawOpts >>= toUIOpts >>= checkUIOpts
|
getHledgerUIOpts = processArgs uimode >>= return . decodeRawOpts >>= rawOptsToUIOpts
|
||||||
|
|
||||||
|
|||||||
@ -297,7 +297,7 @@ defaultWidth = 80
|
|||||||
-- today's date. Parsing failures will raise an error.
|
-- today's date. Parsing failures will raise an error.
|
||||||
-- Also records the terminal width, if supported.
|
-- Also records the terminal width, if supported.
|
||||||
rawOptsToCliOpts :: RawOpts -> IO CliOpts
|
rawOptsToCliOpts :: RawOpts -> IO CliOpts
|
||||||
rawOptsToCliOpts rawopts = do
|
rawOptsToCliOpts rawopts = checkCliOpts <$> do
|
||||||
ropts <- rawOptsToReportOpts rawopts
|
ropts <- rawOptsToReportOpts rawopts
|
||||||
mcolumns <- readMay <$> getEnvSafe "COLUMNS"
|
mcolumns <- readMay <$> getEnvSafe "COLUMNS"
|
||||||
mtermwidth <-
|
mtermwidth <-
|
||||||
@ -325,13 +325,14 @@ rawOptsToCliOpts rawopts = do
|
|||||||
}
|
}
|
||||||
|
|
||||||
-- | Do final validation of processed opts, raising an error if there is trouble.
|
-- | Do final validation of processed opts, raising an error if there is trouble.
|
||||||
checkCliOpts :: CliOpts -> IO CliOpts -- or pure..
|
checkCliOpts :: CliOpts -> CliOpts
|
||||||
checkCliOpts opts@CliOpts{reportopts_=ropts} = do
|
checkCliOpts opts =
|
||||||
case lineFormatFromOpts ropts of
|
either optserror (const opts) $ do
|
||||||
Left err -> optserror $ "could not parse format option: "++err
|
-- XXX move to checkReportOpts or move _format to CliOpts
|
||||||
Right _ -> return ()
|
case lineFormatFromOpts $ reportopts_ opts of
|
||||||
|
Left err -> Left $ "could not parse format option: "++err
|
||||||
|
Right _ -> Right ()
|
||||||
-- XXX check registerWidthsFromOpts opts
|
-- XXX check registerWidthsFromOpts opts
|
||||||
return opts
|
|
||||||
|
|
||||||
-- Currently only used by some extras/ scripts:
|
-- Currently only used by some extras/ scripts:
|
||||||
-- | Parse hledger CLI options from the command line using the given
|
-- | Parse hledger CLI options from the command line using the given
|
||||||
@ -341,7 +342,7 @@ getCliOpts :: Mode RawOpts -> IO CliOpts
|
|||||||
getCliOpts mode' = do
|
getCliOpts mode' = do
|
||||||
args' <- getArgs
|
args' <- getArgs
|
||||||
let rawopts = decodeRawOpts $ processValue mode' args'
|
let rawopts = decodeRawOpts $ processValue mode' args'
|
||||||
opts <- rawOptsToCliOpts rawopts >>= checkCliOpts
|
opts <- rawOptsToCliOpts rawopts
|
||||||
debugArgs args' opts
|
debugArgs args' opts
|
||||||
-- if any (`elem` args) ["--help","-h","-?"]
|
-- if any (`elem` args) ["--help","-h","-?"]
|
||||||
when ("help" `inRawOpts` rawopts_ opts) $
|
when ("help" `inRawOpts` rawopts_ opts) $
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user