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 :: Day -> UTCTime
dayToUTC d = localTimeToUTC utc (LocalTime d midnight) dayToUTC d = localTimeToUTC utc (LocalTime d midnight)
-- | Convert a smart date string to a date span using the provided date as -- | Convert a period expression to a date span using the provided reference date.
-- reference point. spanFromPeriodExpr refdate = spanFromSmartDateString refdate
-- | Convert a smart date string to a date span using the provided reference date.
spanFromSmartDateString :: Day -> String -> DateSpan spanFromSmartDateString :: Day -> String -> DateSpan
spanFromSmartDateString refdate s = DateSpan (Just b) (Just e) spanFromSmartDateString refdate s = DateSpan (Just b) (Just e)
where 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) 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 -- | 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 :: Day -> String -> String
fixSmartDateStr t s = printf "%04d/%02d/%02d" y m d fixSmartDateStr t s = printf "%04d/%02d/%02d" y m d
where where
(y,m,d) = toGregorian $ fixSmartDate t sdate (y,m,d) = toGregorian $ fixSmartDate t sdate
sdate = fromparse $ parsewith smartdate $ map toLower s sdate = fromparse $ parsewith smartdate $ map toLower s
-- | Convert a SmartDate to an absolute date using the provided date as -- | Convert a SmartDate to an absolute date using the provided reference date.
-- reference point.
fixSmartDate :: Day -> SmartDate -> Day fixSmartDate :: Day -> SmartDate -> Day
fixSmartDate refdate sdate = fix sdate fixSmartDate refdate sdate = fix sdate
where where

View File

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