Remove --separator command line argument

This commit is contained in:
Aleksandar Dimitrov 2020-01-05 18:56:04 +01:00 committed by Simon Michael
parent 93c27891f2
commit 8df4e1ed83
4 changed files with 24 additions and 61 deletions

View File

@ -50,7 +50,6 @@ module Hledger.Read.Common (
getAccountAliases,
clearAccountAliases,
journalAddFile,
getSpecialSeparators,
-- * parsers
-- ** transaction bits
@ -163,7 +162,6 @@ data InputOpts = InputOpts {
mformat_ :: Maybe StorageFormat -- ^ a file/storage format to try, unless overridden
-- by a filename prefix. Nothing means try all.
,mrules_file_ :: Maybe FilePath -- ^ a conversion rules file to use (when reading CSV)
,separator_ :: Maybe Char -- ^ the separator to use (when reading CSV)
,aliases_ :: [String] -- ^ account name aliases to apply
,anon_ :: Bool -- ^ do light anonymisation/obfuscation of the data
,ignore_assertions_ :: Bool -- ^ don't check balance assertions
@ -176,14 +174,13 @@ data InputOpts = InputOpts {
instance Default InputOpts where def = definputopts
definputopts :: InputOpts
definputopts = InputOpts def def def def def def def True def def
definputopts = InputOpts def def def def def def True def def
rawOptsToInputOpts :: RawOpts -> InputOpts
rawOptsToInputOpts rawopts = InputOpts{
-- files_ = listofstringopt "file" rawopts
mformat_ = Nothing
,mrules_file_ = maybestringopt "rules-file" rawopts
,separator_ = maybestringopt "separator" rawopts >>= getSpecialSeparators
,aliases_ = listofstringopt "alias" rawopts
,anon_ = boolopt "anon" rawopts
,ignore_assertions_ = boolopt "ignore-assertions" rawopts
@ -195,14 +192,6 @@ rawOptsToInputOpts rawopts = InputOpts{
--- * parsing utilities
-- | Parse special separator names TAB and SPACE, or return the first
-- character. Return Nothing on empty string
getSpecialSeparators :: String -> Maybe Char
getSpecialSeparators "SPACE" = Just ' '
getSpecialSeparators "TAB" = Just '\t'
getSpecialSeparators (x:_) = Just x
getSpecialSeparators [] = Nothing
-- | Run a text parser in the identity monad. See also: parseWithState.
runTextParser, rtp
:: TextParser Identity a -> Text -> Either (ParseErrorBundle Text CustomErr) a

View File

@ -71,7 +71,7 @@ import Text.Printf (printf)
import Hledger.Data
import Hledger.Utils
import Hledger.Read.Common (Reader(..),InputOpts(..),amountp, statusp, genericSourcePos, getSpecialSeparators, finaliseJournal)
import Hledger.Read.Common (Reader(..),InputOpts(..),amountp, statusp, genericSourcePos, finaliseJournal)
type CSV = [Record]
@ -93,8 +93,7 @@ reader = Reader
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
parse iopts f t = do
let rulesfile = mrules_file_ iopts
let separator = separator_ iopts
r <- liftIO $ readJournalFromCsv separator rulesfile f t
r <- liftIO $ readJournalFromCsv rulesfile f t
case r of Left e -> throwError e
Right pj -> finaliseJournal iopts{ignore_assertions_=True} f t pj'
where
@ -104,12 +103,19 @@ parse iopts f t = do
-- better preemptively reverse them once more. XXX inefficient
pj' = journalReverse pj
-- | Parse special separator names TAB and SPACE, or return the first
-- character. Return Nothing on empty string
getSpecialSeparators :: String -> Maybe Char
getSpecialSeparators "SPACE" = Just ' '
getSpecialSeparators "TAB" = Just '\t'
getSpecialSeparators (x:_) = Just x
getSpecialSeparators [] = Nothing
-- | Decide which separator to get.
-- If the external separator is provided, take it. Otherwise, look at the rules. Finally, return ','.
getSeparator :: Maybe Char -> CsvRules -> Char
getSeparator externalSeparator rules = head $
catMaybes [ externalSeparator
, getDirective "separator" rules >>= getSpecialSeparators
getSeparator :: CsvRules -> Char
getSeparator rules = head $
catMaybes [ getDirective "separator" rules >>= getSpecialSeparators
, Just ',']
-- | Read a Journal from the given CSV data (and filename, used for error
@ -123,9 +129,9 @@ getSeparator externalSeparator rules = head $
-- 4. if the rules file didn't exist, create it with the default rules and filename
-- 5. return the transactions as a Journal
-- @
readJournalFromCsv :: Maybe Char -> Maybe FilePath -> FilePath -> Text -> IO (Either String Journal)
readJournalFromCsv _ Nothing "-" _ = return $ Left "please use --rules-file when reading CSV from stdin"
readJournalFromCsv commandLineSeparator mrulesfile csvfile csvdata =
readJournalFromCsv :: Maybe FilePath -> FilePath -> Text -> IO (Either String Journal)
readJournalFromCsv Nothing "-" _ = return $ Left "please use --rules-file when reading CSV from stdin"
readJournalFromCsv mrulesfile csvfile csvdata =
handle (\(e::IOException) -> return $ Left $ show e) $ do
-- make and throw an IO exception.. which we catch and convert to an Either above ?
@ -156,7 +162,7 @@ readJournalFromCsv commandLineSeparator mrulesfile csvfile csvdata =
records <- (either throwerr id .
dbg2 "validateCsv" . validateCsv rules skiplines .
dbg2 "parseCsv")
`fmap` parseCsv (getSeparator commandLineSeparator rules) parsecfilename csvdata
`fmap` parseCsv (getSeparator rules) parsecfilename csvdata
dbg1IO "first 3 csv records" $ take 3 records
-- identify header lines

View File

@ -122,7 +122,6 @@ inputflags :: [Flag RawOpts]
inputflags = [
flagReq ["file","f"] (\s opts -> Right $ setopt "file" s opts) "FILE" "use a different input file. For stdin, use - (default: $LEDGER_FILE or $HOME/.hledger.journal)"
,flagReq ["rules-file"] (\s opts -> Right $ setopt "rules-file" s opts) "RFILE" "CSV conversion rules file (default: FILE.rules)"
,flagReq ["separator"] (\s opts -> Right $ setopt "separator" s opts) "SEP" "CSV separator (default: ,)"
,flagReq ["alias"] (\s opts -> Right $ setopt "alias" s opts) "OLD=NEW" "rename accounts named OLD to NEW"
,flagNone ["anon"] (setboolopt "anon") "anonymize accounts and payees"
,flagReq ["pivot"] (\s opts -> Right $ setopt "pivot" s opts) "TAGNAME" "use some other field/tag for account names"

View File

@ -152,13 +152,14 @@ $ ./hledger-csv
RULES
account1 Assets:MyAccount
date %1
separator ;
date-format %d/%Y/%m
description %2
amount-in %3
amount-out %4
currency $
$ ./hledger-csv --separator ';'
$ ./hledger-csv
2009/09/10 Flubber Co🎅
Assets:MyAccount $50
income:unknown $-50
@ -527,7 +528,7 @@ $ ./hledger-csv
>=0
# 25. specify alternative whitespace delimiter in rules
# 25. specify reserved word whitespace separator in rules
<
2009/10/01 Flubber Co 50 123
@ -543,39 +544,7 @@ $ ./hledger-csv
>=0
# 26. specify char delimiter in rules
<
2009/10/01;Flubber Co;50;123
RULES
fields date, description, amount, balance
currency $
account1 (assets:myacct)
separator ;
$ ./hledger-csv
2009/10/01 Flubber Co
(assets:myacct) $50 = $123
>=0
# 27. command line delimiter overrides configuration file
<
2009/10/01 Flubber Co 50 123
RULES
fields date, description, amount, balance
currency $
account1 (assets:myacct)
separator ;
$ ./hledger-csv --separator 'TAB'
2009/10/01 Flubber Co
(assets:myacct) $50 = $123
>=0
## 28. A single unbalanced posting with number other than 1 also should not generate a balancing posting.
## 26. A single unbalanced posting with number other than 1 also should not generate a balancing posting.
#<
#2019-01-01,1
#
@ -589,7 +558,7 @@ $ ./hledger-csv --separator 'TAB'
#
#>=0
#
## 29. A single posting that's zero also should not generate a balancing posting.
## 27. A single posting that's zero also should not generate a balancing posting.
#<
#2019-01-01,0
#
@ -603,7 +572,7 @@ $ ./hledger-csv --separator 'TAB'
#
#>=0
## 30. With a bracketed account name, the auto-generated second posting should also be bracketed.
## 28. With a bracketed account name, the auto-generated second posting should also be bracketed.
#<
#2019-01-01,1
#