summaryrefslogtreecommitdiff
path: root/config.cc
blob: 56b5676105b89f65b0eed27e9ec90f70bce7a10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "config.h"
#include "option.h"

namespace ledger {

config_t * config = NULL;

const std::string bal_fmt	 = "%20T  %2_%-n\n";
const std::string reg_fmt
  = "%D %-.20P %-.22N %12.66t %12.80T\n\
%/                                %-.22N %12.66t %12.80T\n";
const std::string plot_value_fmt = "%D %t\n";
const std::string plot_total_fmt = "%D %T\n";
const std::string print_fmt
  = "\n%D %X%C%P\n    %-34N  %12o\n%/    %-34N  %12o\n";
const std::string equity_fmt
  = "\n%D %X%C%P\n%/    %-34N  %12t\n";

config_t::config_t()
{
  if (const char * p = std::getenv("HOME"))
    init_file = cache_file = price_db = p;

  init_file  += "/.ledgerrc";
  cache_file += "/.ledger";
  price_db   += "/.pricedb";

  value_expr	     = "a";
  total_expr	     = "O";
  pricing_leeway     = 24 * 3600;
  show_collapsed     = false;
  show_subtotal      = false;
  show_related       = false;
  show_inverted      = false;
  show_empty	     = false;
  days_of_the_week   = false;
  show_revalued      = false;
  show_revalued_only = false;
  download_quotes    = false;
}

static void show_version(std::ostream& out)
{
  out
    << "Ledger " << ledger::version << ", the command-line accounting tool\n\n"
    << "Copyright (c) 2003-2004, New Artisans LLC. All rights reserved.\n\n"
    << "This program is made available under the terms of the BSD Public\n"
    << "License.  See the LICENSE file included with the distribution for\n"
    << "details and disclaimer.\n";
}

void option_help(std::ostream& out)
{
  out
    << "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\
Basic options:\n\
  -h, --help            display this help text\n\
  -v, --version         show version information\n\
  -i, --init FILE       initialize ledger by loading FILE (def: ~/.ledgerrc)\n\
  -f, --file FILE       read ledger data from FILE\n\
  -o, --output FILE     write output to FILE\n\
  -p, --set-price CONV  specify a commodity conversion: \"COMM=AMOUNT\"\n\n\
Report filtering:\n\
  -b, --begin-date DATE set report begin date\n\
  -e, --end-date DATE   set report end date\n\
  -c, --current         show only current and past entries (not future)\n\
  -C, --cleared         consider only cleared transactions\n\
  -U, --uncleared       consider only uncleared transactions\n\
  -R, --real            consider only non-virtual transactions\n\n\
  -r, --related         calculate report using related transactions\n\
Output customization:\n\
  -F, --format STR      use STR as the report format\n\
  -y, --date-format STR use STR as the date format (def: %Y/%m/%d)\n\
  -E, --empty           balance: show accounts with zero balance\n\
  -n, --collapse        register: collapse entries with multiple transactions\n\
  -s, --subtotal        balance: show sub-accounts; register: show subtotals\n\
  -S, --sort EXPR       sort report according to the value expression EXPR\n\
  -z, --interval STR    report by interval, based on interval expression STR\n\
  -w, --dow             show a days-of-the-week report\n\
  -W, --weekly          show weekly sub-totals\n\
  -M, --monthly         show monthly sub-totals\n\
  -Y, --yearly          show yearly sub-totals\n\
  -l, --limit EXPR      calculate only transactions matching EXPR\n\
  -d, --display EXPR    display only transactions matching EXPR\n\
  -t, --value EXPR      set the value expression for all report types\n\
  -T, --total EXPR      set the total expression for all report types\n\
  -j, --value-data      print only raw value data (useful when scripting)\n\
  -J, --total-data      print only raw total data\n\n\
Commodity reporting:\n\
  -P, --price-db FILE   sets the price database to FILE (def: ~/.pricedb)\n\
  -L, --price-exp MINS  download quotes only if newer than MINS (def: 1440)\n\
  -Q, --download        download price information when needed\n\
  -O, --quantity        report commodity totals (this is the default)\n\
  -B, --basis           report commodity cost basis\n\
  -V, --market          report commodity market value\n\
  -G, --gain            report commodity gain/loss\n\
  -A, --average         report average transaction amount\n\
  -D, --deviation       report deviation from the average\n\
  -X, --trend           report average deviation from the average\n\
  -Z, --weighted-trend  same as trend, but older values are less significant\n\
			(-D, -X and -Z make little sense in balance reports)\n\n\
Commands:\n\
  balance [REGEXP]...   show balance totals for matching accounts\n\
  register [REGEXP]...  show register of matching transactions\n\
  print [REGEXP]...     print all matching entries\n\
  equity [REGEXP]...    output equity entries for matching accounts\n\
  entry DATE PAYEE AMT  output a derived entry, based on the arguments\n\n\
For commands that accepts a list of regular expressions, these match against\n\
the account.  If the separator \"--\" is specified, regexps after it are\n\
matched against the payee name.  For even more sophisticated entry matching,\n\
use --limit; to affect display only (not calculation), use --display.\n";
}

//////////////////////////////////////////////////////////////////////
//
// Basic options

DEF_OPT_HANDLERS();

OPT_BEGIN(help, "h") {
  option_help(std::cout);
  std::exit(0);
} OPT_END(help);

OPT_BEGIN(version, "v") {
  show_version(std::cout);
  std::exit(0);
} OPT_END(version);

OPT_BEGIN(init, "i:") {
  config->init_file = optarg;
} OPT_END(init);

OPT_BEGIN(file, "f:") {
  char * buf = new char[std::strlen(optarg) + 1];
  std::strcpy(buf, optarg);
  for (char * p = std::strtok(buf, ":");
       p;
       p = std::strtok(NULL, ":"))
    config->files.push_back(p);
  delete[] buf;
} OPT_END(file);

OPT_BEGIN(cache, ":") {
  config->cache_file = optarg;
} OPT_END(cache);

OPT_BEGIN(output, "o:") {
  if (std::string(optarg) != "-")
    config->output_file = optarg;
} OPT_END(output);

OPT_BEGIN(set_price, "p:") {
  if (std::strchr(optarg, '='))
    config->price_settings.push_back(optarg);
  else
    std::cerr << "Error: Invalid price setting: " << optarg << std::endl;
} OPT_END(set_price);

//////////////////////////////////////////////////////////////////////
//
// Report filtering

OPT_BEGIN(begin_date, "b:") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "d>=[";
  config->predicate += optarg;
  config->predicate += "]";
} OPT_END(begin_date);

OPT_BEGIN(end_date, "e:") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "d<[";
  config->predicate += optarg;
  config->predicate += "]";
} OPT_END(end_date);

OPT_BEGIN(current, "c") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "d<=N";
} OPT_END(current);

OPT_BEGIN(cleared, "C") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "X";
} OPT_END(cleared);

OPT_BEGIN(uncleared, "U") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "!X";
} OPT_END(uncleared);

OPT_BEGIN(real, "R") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "R";
} OPT_END(real);

//////////////////////////////////////////////////////////////////////
//
// Output customization

OPT_BEGIN(format, "F:") {
  config->format_string = optarg;
} OPT_END(format);

OPT_BEGIN(date_format, "y:") {
  config->date_format = optarg;
} OPT_END(date_format);

OPT_BEGIN(empty, "E") {
  config->show_empty = true;
} OPT_END(empty);

OPT_BEGIN(collapse, "n") {
  config->show_collapsed = true;
} OPT_END(collapse);

OPT_BEGIN(subtotal, "s") {
  config->show_subtotal = true;
} OPT_END(subtotal);

OPT_BEGIN(sort, "S:") {
  config->sort_string = optarg;
} OPT_END(sort);

OPT_BEGIN(related, "r") {
  config->show_related = true;
} OPT_END(related);

OPT_BEGIN(interval, "z:") {
  config->interval_text = optarg;
} OPT_END(interval);

OPT_BEGIN(weekly, "W") {
  config->interval_text = "weekly";
} OPT_END(weekly);

OPT_BEGIN(dow, "w") {
  config->days_of_the_week = true;
} OPT_END(dow);

OPT_BEGIN(monthly, "M") {
  config->interval_text = "monthly";
} OPT_END(monthly);

OPT_BEGIN(yearly, "Y") {
  config->interval_text = "yearly";
} OPT_END(yearly);

OPT_BEGIN(limit, "l:") {
  if (! config->predicate.empty())
    config->predicate += "&";
  config->predicate += "(";
  config->predicate += optarg;
  config->predicate += ")";
} OPT_END(limit);

OPT_BEGIN(display, "d:") {
  if (! config->display_predicate.empty())
    config->display_predicate += "&";
  config->display_predicate += "(";
  config->display_predicate += optarg;
  config->display_predicate += ")";
} OPT_END(display);

OPT_BEGIN(value, "t:") {
  config->value_expr = optarg;
} OPT_END(value);

OPT_BEGIN(total, "T:") {
  config->total_expr = optarg;
} OPT_END(total);

OPT_BEGIN(value_data, "j") {
  config->value_expr    = "S" + config->value_expr;
  config->format_string = plot_value_fmt;
} OPT_END(value_data);

OPT_BEGIN(total_data, "J") {
  config->total_expr    = "S" + config->total_expr;
  config->format_string = plot_total_fmt;
} OPT_END(total_data);

//////////////////////////////////////////////////////////////////////
//
// Commodity reporting

OPT_BEGIN(price_db, "P:") {
  config->price_db = optarg;
} OPT_END(price_db);

OPT_BEGIN(price_exp, "L:") {
  config->pricing_leeway = std::atol(optarg) * 60;
} OPT_END(price_exp);

OPT_BEGIN(download, "Q") {
  config->download_quotes = true;
} OPT_END(download);

OPT_BEGIN(quantity, "O") {
  config->value_expr = "a";
  config->total_expr = "O";
} OPT_END(quantity);

OPT_BEGIN(basis, "B") {
  config->value_expr = "c";
  config->total_expr = "C";
} OPT_END(basis);

OPT_BEGIN(market, "V") {
  config->show_revalued = true;

  config->value_expr = "v";
  config->total_expr = "V";
} OPT_END(market);

OPT_BEGIN(gain, "G") {
  config->show_revalued      =
  config->show_revalued_only = true;

  config->value_expr = "a";
  config->total_expr = "G";
} OPT_END(gain);

OPT_BEGIN(average, "A") {
  config->value_expr = "a";
  config->total_expr = "MO";
} OPT_END(average);

OPT_BEGIN(deviation, "D") {
  config->value_expr = "a";
  config->total_expr = "DMO";
} OPT_END(deviation);

OPT_BEGIN(trend, "X") {
  config->value_expr = "a";
  config->total_expr = "MDMO";
} OPT_END(trend);

OPT_BEGIN(weighted_trend, "Z") {
  config->value_expr = "a";
  config->total_expr
    = "MD(MO/(1+(((N-d)/(30*86400))<0?0:((N-d)/(30*86400)))))";
} OPT_END(weighted_trend);

} // namespace ledger