Class: Bankjob::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bankjob/cli.rb

Constant Summary collapse

NEEDED =

constant to indicate compulsory options

"Needed"
NOT_NEEDED =

constant to indicate no-longer compulsory options

"Not Needed"

Class Method Summary collapse

Class Method Details

.execute(stdout, argv) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/bankjob/cli.rb', line 17

def self.execute(stdout, argv)
  # The BanjobOptions module above, through the magic of OptiFlags
  # has augmented ARGV with the command line options accessible through
  # ARGV.flags.
  runner = BankjobRunner.new()
  runner.run(parse(argv), stdout)
end

.parse(args) ⇒ Object

Parses the command line arguments using OptionParser and returns an open struct with an attribute for each option



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
# File 'lib/bankjob/cli.rb', line 29

def self.parse(args)
  options = OpenStruct.new

  # Set the default options
  options.scraper = NEEDED
  options.scraper_args = []
  options.log_level = Logger::WARN
  options.log_file = nil
  options.debug = false
  options.input = nil
  options.ofx = false # ofx is the default but only if csv is false
  options.ofx_out = false
  options.csv = false
  options.csv_out = nil # allow for separate csv and ofx output files
  options.wesabe_help = false
  options.wesabe_upload = false
  options.wesabe_args = nil
  options.logger = nil

  opt = OptionParser.new do |opt|
  
    opt.banner = "Bankjob - scrapes your online banking website and produces an OFX or CSV document.\n" +
                 "Usage: bankjob [options]\n"

    opt.version = Bankjob::BANKJOB_VERSION
  
    opt.on('-s', '--scraper SCRAPER',
           "The name of the ruby file that scrapes the website.\n") do |file|
      options.scraper = file
    end

    opt.on('--scraper-args ARGS',
           "Any arguments you want to pass on to your scraper.",
           "The entire set of arguments must be quoted and separated by spaces",
           "but you can use single quotes to specify multi-word arguments for",
           "your scraper.  E.g.",
           "   -scraper-args \"-user Joe -password Joe123 -arg3 'two words'\""," ",
           "This assumes your scraper accepts an array of args and knows what",
           "to do with them, it will vary from scraper to scraper.\n") do |sargs|
      options.scraper_args = sub_args_to_array(sargs)
    end

    opt.on('-i', '--input INPUT_HTML_FILE',
           "An html file used as the input instead of scraping the website -",
           "useful for debugging.\n") do |file|
      options.input = file
    end

    opt.on('-l', '--log LOG_FILE',
           "Specify a file to log information and debug messages.",
           "If --debug is used, log info will go to the console, but if neither",
           "this nor --debug is specfied, there will be no log.",
           "Note that the log is rolled over once per week\n") do |log_file|
      options.log_file = log_file
    end

    opt.on('q', '--quiet', "Suppress all messages, warnings and errors.",
           "Only fatal errors will go in the log") do
      options.log_level = Logger::FATAL
    end

    opt.on( '--verbose', "Log detailed informational messages.\n") do
      options.log_level = Logger::INFO
    end

    opt.on('--debug',
           "Log debug-level information to the log",
           "if here is one and put debug info in log\n") do
      options.log_level = Logger::DEBUG
      options.debug = true
    end

    opt.on('--ofx [FILE]',
           "Write out the statement as an OFX2 compliant XML document."," ",
           "If FILE is not specified, the XML is dumped to the console.",
           "If FILE specifies a directory then a new file will be created with a",
           "name generated from the dates of the first and last transactions.",
           "If FILE specifies a file that already exists it will be overwritten."," ",
           "(Note that ofx is the default format unless --csv is specified,",
           "and that both CSV and OFX documents can be produced by specifying",
           "both options.)\n") do |file|
      options.ofx = true
      options.ofx_out = file
    end

    opt.on('--csv [FILE]',
           "Writes out the statement as a CSV (comma separated values) document.",
           "All of the information available including numeric values for amount,",
           "raw and rule-generated descriptions, etc, are produced in the CSV document.", " ",
           "The document produced is suitable for loading into a spreadsheet like",
           "Microsoft Excel with the dates formatted to allow for auto recognition.",
           "This option can be used in conjunction with --ofx or --wesabe to produce",
           "a local permanent log of all the data scraped over time.", " ",
           "If FILE is not specified, the CSV is dumped to the console.",
           "If FILE specifies a directory then a new file will be created with a",
           "name generated from the dates of the first and last transactions.",
           "If FILE specifies a file that already exists then the new statement",
           "will be appended to the existing one in that file with care taken to",
           "merge removing duplicate entries.\n",
           "[WARNING - this merging does not yet function properly - its best to specify a directory for now.]\n"
         ) do |file|
      # TODO update this warning when we have merging working
      options.csv = true
      options.csv_out = file
    end

    opt.on('--wesabe-help [WESABE_ARGS]',
           "Show help information on how to use Bankjob to upload to Wesabe.",
           "Optionally use with \"wesabe-user password\" to get Wesabe account info.",
           "Note that the quotes around the WESABE_ARGS to send both username",
           "and password are necessary.", " ",
           "Use --wesabe-help with no args for more details.\n") do |wargs|
      options.wesabe_args = sub_args_to_array(wargs)
      options.wesabe_help = true
      options.scraper = NOT_NEEDED # scraper is not NEEDED when this option is set
    end

    opt.on('--wesabe WESABE_ARGS',
           "Produce an OFX document from the statement and upload it to a Wesabe account.",
           "WESABE_ARGS must be quoted and space-separated, specifying the wesabe account",
           "username, password and - if there is more than one - the wesabe account number.", " ",
           "Before trying this, use bankjob --wesabe-help to get more information.\n"
         ) do |wargs|
      options.wesabe_args = sub_args_to_array(wargs)
      options.wesabe_upload = true
    end

    opt.on('--version', "Display program version and exit.\n" ) do
      puts opt.version
      exit
    end
 
    opt.on_tail('-h', '--help', "Display this usage message and exit.\n" ) do
      puts opt
      puts <<-EOF

  Some common options:

o Debugging:
  --debug --scraper bpi_scraper.rb --input /tmp/DownloadedPage.html --ofx
 
o Regular use: (output ofx documents to a directory called 'bank')
  --scraper /bank/mybank_scraper.rb --scraper-args "me mypass123" --ofx /bank --log /bank/bankjob.log --verbose
  
o Abbreviated options with CSV output: (output csv appended continuously to a file)
  -s /bank/otherbank_scraper.rb --csv /bank/statements.csv -l /bank/bankjob.log -q

o Get help on using Wesabe:
  --wesabe-help

o Upload to Wesabe: (I have 4 Wesabe accounts and am uploading to the 3rd)
  -s /bank/mybank_scraper.rb --wesabe "mywesabeuser password 3"  -l /bank/bankjob.log --debug
  EOF
      exit!
    end
  
  end #OptionParser.new

  begin
    opt.parse!(args)
    _validate_options(options) # will raise exceptions if options are invalid
    _init_logger(options) # sets the logger
  rescue Exception => e
    puts e, "", opt
    exit
  end
  
  return options
end