options cleanup

This commit is contained in:
Simon Michael 2008-11-27 06:48:46 +00:00
parent 57c31f5ab0
commit c3bec2a3de
2 changed files with 15 additions and 70 deletions

View File

@ -53,8 +53,10 @@ elapsedSeconds t1 t2 = realToFrac $ diffUTCTime t1 t2
dayToUTC :: Day -> UTCTime
dayToUTC d = localTimeToUTC utc (LocalTime d midnight)
-- | Convert a smart date string to a date span using the provided date as
-- reference point.
-- | Convert a period expression to a date span using the provided reference date.
spanFromPeriodExpr refdate = spanFromSmartDateString refdate
-- | Convert a smart date string to a date span using the provided reference date.
spanFromSmartDateString :: Day -> String -> DateSpan
spanFromSmartDateString refdate s = DateSpan (Just b) (Just e)
where
@ -88,15 +90,14 @@ spanFromSmartDateString refdate s = DateSpan (Just b) (Just e)
span (y,m,d) = (day, nextday day) where day = fromGregorian (read y) (read m) (read d)
-- | Convert a smart date string to an explicit yyyy/mm/dd string using
-- the provided date as reference point.
-- the provided reference date.
fixSmartDateStr :: Day -> String -> String
fixSmartDateStr t s = printf "%04d/%02d/%02d" y m d
where
(y,m,d) = toGregorian $ fixSmartDate t sdate
sdate = fromparse $ parsewith smartdate $ map toLower s
-- | Convert a SmartDate to an absolute date using the provided date as
-- reference point.
-- | Convert a SmartDate to an absolute date using the provided reference date.
fixSmartDate :: Day -> SmartDate -> Day
fixSmartDate refdate sdate = fix sdate
where

View File

@ -126,76 +126,26 @@ dateSpanFromOpts refdate opts
parse s = parsedate $ printf "%04s/%02s/%02s" y m d
where (y,m,d) = fromparse $ parsewith smartdate $ s
spanFromPeriodExpr refdate = spanFromSmartDateString refdate
-- | Get the value of the begin date option, if any.
beginDateFromOpts :: [Opt] -> Maybe Day
beginDateFromOpts opts =
if null beginopts
then Nothing
else Just $ parsedate $ printf "%04s/%02s/%02s" y m d
where
beginopts = concatMap getbegindate opts
getbegindate (Begin s) = [s]
getbegindate _ = []
defaultdate = ""
(y,m,d) = fromparse $ parsewith smartdate $ last beginopts
-- | Get the value of the end date option, if any.
endDateFromOpts :: [Opt] -> Maybe Day
endDateFromOpts opts =
if null endopts
then Nothing
else Just $ parsedate $ printf "%04s/%02s/%02s" y m d
where
endopts = concatMap getenddate opts
getenddate (End s) = [s]
getenddate _ = []
defaultdate = ""
(y,m,d) = fromparse $ parsewith smartdate $ last endopts
-- | Get the value of the period option, if any.
periodFromOpts :: [Opt] -> Maybe String
periodFromOpts opts =
if null periodopts
then Nothing
else Just $ head periodopts
where
periodopts = concatMap getperiod opts
getperiod (Period s) = [s]
getperiod _ = []
-- | Get the value of the depth option, if any.
-- | Get the value of the (first) depth option, if any.
depthFromOpts :: [Opt] -> Maybe Int
depthFromOpts opts =
case depthopts of
(x:_) -> Just $ read x
_ -> Nothing
depthFromOpts opts = listtomaybeint $ optValuesForConstructor Depth opts
where
depthopts = concatMap getdepth opts
getdepth (Depth s) = [s]
getdepth _ = []
listtomaybeint [] = Nothing
listtomaybeint vs = Just $ read $ head vs
-- | Get the value of the display option, if any.
-- | Get the value of the (first) display option, if any.
displayFromOpts :: [Opt] -> Maybe String
displayFromOpts opts =
case displayopts of
(s:_) -> Just s
_ -> Nothing
displayFromOpts opts = listtomaybe $ optValuesForConstructor Display opts
where
displayopts = concatMap getdisplay opts
getdisplay (Display s) = [s]
getdisplay _ = []
listtomaybe [] = Nothing
listtomaybe vs = Just $ head vs
-- | Get the ledger file path from options, an environment variable, or a default
ledgerFilePathFromOpts :: [Opt] -> IO String
ledgerFilePathFromOpts opts = do
envordefault <- getEnv fileenvvar `catch` \_ -> return defaultfile
paths <- mapM tildeExpand $ [envordefault] ++ (concatMap getfile opts)
paths <- mapM tildeExpand $ [envordefault] ++ optValuesForConstructor File opts
return $ last paths
where
getfile (File s) = [s]
getfile _ = []
-- | Expand ~ in a file path (does not handle ~name).
tildeExpand :: FilePath -> IO FilePath
@ -221,9 +171,3 @@ parseAccountDescriptionArgs opts args = (as, ds')
negchar
| OptionsAnywhere `elem` opts = '^'
| otherwise = '-'
testoptions order cmdline = putStr $
case getOpt order options cmdline of
(o,n,[] ) -> "options=" ++ show o ++ " args=" ++ show n
(o,_,errs) -> concat errs ++ usage