class Rledger::Runner

Public Class Methods

parse(args) click to toggle source

parse command line

# 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) click to toggle source

run: interpret command line and execute

# 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