-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathargparse.go
199 lines (181 loc) · 5.67 KB
/
argparse.go
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
package main
import "os"
import "fmt"
import "flag"
import "time"
import "strconv"
import "github.com/prataprc/goparsec"
import "github.com/bnclabs/golog"
import "github.com/tn47/goledger/api"
import "github.com/tn47/goledger/dblentry"
func argparse() ([]string, error) {
var journals, outfile, finyear, begindt, enddt string
f := flag.NewFlagSet("ledger", flag.ExitOnError)
f.Usage = func() {
fmsg := "Usage of command: %v [OPTIONS] COMMAND [ARGS]\n"
fmt.Printf(fmsg, os.Args[0])
f.PrintDefaults()
}
f.StringVar(&api.Options.Dbname, "db", "devjournal",
"Provide datastore name")
f.StringVar(&journals, "f", "example/first.ldg",
"Comma separated list of input files.")
f.StringVar(&outfile, "o", "",
"outfile to report")
f.StringVar(&api.Options.Currentdt, "current", "",
"Display only transactions on or before the current date.")
f.StringVar(&begindt, "begin", "",
"Display only transactions on or before the current date.")
f.StringVar(&enddt, "end", "",
"Display only transactions on or before the current date.")
f.StringVar(&finyear, "fy", "",
"financial year.")
f.StringVar(&api.Options.Period, "period", "",
"Limit the processing to transactions in PERIOD_EXPRESSION.")
f.BoolVar(&api.Options.Nosubtotal, "nosubtotal", false,
"Don't accumulate postings on sub-leger to parent ledger.")
f.BoolVar(&api.Options.Subtotal, "subtotal", false,
"all transactions to be collapsed into a single, transaction")
f.BoolVar(&api.Options.Cleared, "cleared", true,
"Display only cleared postings.")
f.BoolVar(&api.Options.Uncleared, "uncleared", true,
"Display only uncleared postings.")
f.BoolVar(&api.Options.Pending, "pending", true,
"Display only pending postings.")
f.BoolVar(&api.Options.Dcformat, "dc", false,
"Display only real postings.")
f.BoolVar(&api.Options.Strict, "strict", false,
"Accounts, tags or commodities not previously declared "+
"will cause warnings.")
f.BoolVar(&api.Options.Pedantic, "pedantic", false,
"Accounts, tags or commodities not previously declared "+
"will cause errors.")
f.BoolVar(&api.Options.Checkpayee, "checkpayee", false,
"Payee not previously declared will cause error.")
f.BoolVar(&api.Options.Stitch, "stitch", false,
"Skip payees with `Opening Balance`")
f.BoolVar(&api.Options.Nopl, "nopl", false,
"skip income and expense accounts")
f.BoolVar(&api.Options.Onlypl, "onlypl", false,
"skip accounts other than income and expense")
f.BoolVar(&api.Options.Detailed, "detailed", false,
"for register, passbook commands list details")
f.BoolVar(&api.Options.Bypayee, "bypayee", false,
"Group postings by common payee names")
f.BoolVar(&api.Options.Daily, "daily", false,
"Group postings by day")
f.BoolVar(&api.Options.Weekly, "weekly", false,
"Group postings by week")
f.BoolVar(&api.Options.Monthly, "monthly", false,
"Group postings by month")
f.BoolVar(&api.Options.Quarterly, "quarterly", false,
"Group postings by quarter")
f.BoolVar(&api.Options.Yearly, "yearly", false,
"Group postings by yearly")
f.BoolVar(&api.Options.Dow, "dow", false,
"Group postings by day of the week")
f.BoolVar(&api.Options.Verbose, "v", false,
"verbose reporting / listing")
f.StringVar(&api.Options.Loglevel, "log", "info",
"Console log level")
f.Parse(os.Args[1:])
logsetts := map[string]interface{}{
"log.level": api.Options.Loglevel,
"log.file": "",
"log.timeformat": "",
"log.prefix": "%v:",
"log.colorfatal": "red",
"log.colorerror": "hired",
"log.colorwarn": "yellow",
}
log.SetLogger(nil, logsetts)
api.Options.Journals = gatherjournals(journals)
api.Options.Outfd = argOutfd(outfile)
endyear := argFinyear(finyear)
if endyear > 0 {
till := time.Date(endyear, 4, 1, 0, 0, 0, 0, time.Local)
ok := api.ValidateDate(till, endyear, 4, 1, 0, 0, 0)
if ok == false {
err := fmt.Errorf("invalid finyear %v", endyear)
log.Errorf("%v\n", err)
return nil, err
}
from := time.Date(endyear-1, 4, 1, 0, 0, 0, 0, time.Local)
ok = api.ValidateDate(from, endyear-1, 4, 1, 0, 0, 0)
if ok == false {
err := fmt.Errorf("invalid begin year for finyear %v", endyear)
log.Errorf("%v\n", err)
return nil, err
}
// Begindt is inclusive, but not Tilldt
api.Options.Begindt, api.Options.Enddt = &from, &till
}
if begindt != "" {
scanner := parsec.NewScanner([]byte(begindt))
node, _ := dblentry.Ydate(time.Now().Year())(scanner)
tm, ok := node.(time.Time)
if ok == false {
err := fmt.Errorf("invalid date %q: %v\n", begindt, node)
log.Errorf("%v\n", err)
return nil, err
}
api.Options.Begindt = &tm
}
if enddt != "" {
scanner := parsec.NewScanner([]byte(enddt))
node, _ := dblentry.Ydate(time.Now().Year())(scanner)
tm, ok := node.(time.Time)
if ok == false {
err := fmt.Errorf("invalid date %q: %v\n", enddt, node)
log.Errorf("%v\n", err)
return nil, err
}
api.Options.Enddt = &tm
}
return f.Args(), nil
}
func gatherjournals(journals string) (files []string) {
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("os.Getwd(): %v\n", err)
os.Exit(1)
}
if journals == "list" {
files, err = listjournals(cwd)
if err != nil {
os.Exit(1)
}
} else if journals == "find" {
files, err = findjournals(cwd)
if err != nil {
os.Exit(1)
}
} else {
files = api.Parsecsv(journals)
}
files = append(files, coveringjournals(cwd)...)
return files
}
func argOutfd(outfile string) *os.File {
outfd := os.Stdout
if outfile != "" {
fd, err := os.Create(outfile)
if err != nil {
log.Errorf("%v\n", err)
os.Exit(1)
}
outfd = fd
}
return outfd
}
func argFinyear(finyear string) int {
if finyear == "" {
return 0
}
fy, err := strconv.Atoi(finyear)
if err != nil {
log.Errorf("arg `-fy` %v\n", err)
os.Exit(1)
}
return fy
}