Class: Financo::CLI::Program::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/financo/cli/program/parser.rb

Overview

Parser

Constant Summary collapse

DEFAULT_CHECKING =
'Bank:Checking'
DEFAULT_OUTPUT =
'journal-<timestamp>.ledger'

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



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
# File 'lib/financo/cli/program/parser.rb', line 11

def initialize
  @op = OptionParser.new
  @op.banner = 'Download and convert N26 bank transactions into a ' \
               'Ledger journal'

  @op.separator(
    StringIO.open do |s|
      s.puts
      s.puts 'Options:'
      s.string
    end
  )

  @op.on(
    '--checking ACCOUNT_NAME',
    "bank checking account (default '#{DEFAULT_CHECKING}')"
  )
  @op.on(
    '-o',
    '--output OUTPUT',
    "journal output: filename or STDOUT (default: #{DEFAULT_OUTPUT})"
  )
  @op.on('-v', '--version', 'show version')
  @op.on('-h', '--help', 'show this message')

  @op.separator(
    StringIO.open do |s|
      s.puts
      s.puts 'Usage:'
      s.puts '  financo [options] <username> <password>'
      s.string
    end
  )
end

Instance Method Details

#helpObject



63
64
65
# File 'lib/financo/cli/program/parser.rb', line 63

def help
  @op.to_s
end

#parse(argv) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/financo/cli/program/parser.rb', line 46

def parse(argv)
  options = {
    checking: DEFAULT_CHECKING,
    output: DEFAULT_OUTPUT.sub('<timestamp>', Time.now.to_i.to_s)
  }
  args = @op.parse(argv, into: options)

  unless options[:help] || options[:version]
    raise ParserError, "expected: '<username> <password>'." if
      args.length != 2
  end

  [args, options]
rescue OptionParser::MissingArgument => e
  raise ParserError, e
end