Class: Rledger::Runner

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

parse command line



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
# File 'lib/rledger.rb', line 12

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

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: rledger [options]"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-c", "--command STRING", String, "Command (one of check, statement, balance)") do |command|
      options[:command] = command
    end

    opts.on("-v", "--voice VOICE", String, "Voice") do |voice|
      options[:voice] = voice
    end

    opts.on("-s", "--sort ASC|DESC", String, "Sort by date") do |sort|
      options[:sort] = sort
    end

    #opts.on("-f", "--from_date DATE", Date, "From date") do |from_date|
    #  options[:from_date] = from_date
    #end

    #opts.on("-to", "--to_date DATE", Date, "To date") do |to_date|
    #  options[:to_date] = to_date
    #end

    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      puts <<EOF
Reads a ledger (http://www.ledger-cli.org) and performs simple operations.

Only the basic format is recognized: do not expect this gem to perform well
with virtual transactions.  Different date formats and different currencies
should, however, be supported.

Example usages

  rledger --command statement ledger.txt
  rledger --command balance ledger.txt

EOF
      exit
    end
  end

  opt_parser.parse!(args)
  options
end

.run(args) ⇒ Object

run: interpret command line and execute



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
# File 'lib/rledger.rb', line 65

def self.run(args)
  # read options
  options = parse(args)

  # read data
  data = []
  ARGV.each do |argv|
    data += Transaction.read(argv)
  end

  # sort, if required from the command line
  if options[:sort] == "ASC" then
    data.sort!
  elsif options[:sort] == "DESC" then
    data.sort.reverse!
  end

  # now do
  case options[:command]
  when "check"
    puts "This is what I understand from the input files:\n"
    data.each do |transaction|
      printf "%s\n", transaction.to_s
    end

  when "statement"
    report = Statement.new(data)
    report.compute(options[:voice])
    puts "Statement of #{options[:voice]}"
    puts report

  when "balance"
    report = Balance.new(data)
    report.compute
    puts report

  else 
    puts "Oh, I wish I could understand what you mean."
    puts "Try with --help."
  end
end