diff --git a/lib/imbecile.rb b/lib/imbecile.rb index b6583cc..513d025 100644 --- a/lib/imbecile.rb +++ b/lib/imbecile.rb @@ -22,10 +22,10 @@ module Imbecile class << self - def run(input_file, output_file) + def run(input_file, output_file, log_file) begin grammar = Grammar.new(File.read(input_file)) - generator = Generator.new(grammar) + generator = Generator.new(grammar, log_file) generator.generate(output_file) rescue Error => e $stderr.puts e.message diff --git a/lib/imbecile/cli.rb b/lib/imbecile/cli.rb index 24570e4..46ecaee 100644 --- a/lib/imbecile/cli.rb +++ b/lib/imbecile/cli.rb @@ -4,6 +4,7 @@ module Imbecile USAGE = < Options: + --log LOG Write log file --version Show program version and exit -h, --help Show this usage and exit EOF @@ -11,9 +12,17 @@ EOF class << self def run(args) - extra_args = [] - args.each do |arg| + params = [] + log_file = nil + i = 0 + while i < args.size + arg = args[i] case arg + when "--log" + if i + 1 < args.size + i += 1 + log_file = args[i] + end when "--version" puts "imbecile v#{VERSION}" return 0 @@ -24,18 +33,19 @@ EOF $stderr.puts "Error: unknown option #{arg}" return 1 else - extra_args << arg + params << arg end + i += 1 end - if extra_args.size != 2 + if params.size != 2 $stderr.puts "Error: specify input and output files" return 1 end - unless File.readable?(args[0]) - $stderr.puts "Error: cannot read #{args[0]}" + unless File.readable?(params[0]) + $stderr.puts "Error: cannot read #{params[0]}" return 2 end - Imbecile.run(*args) + Imbecile.run(*params, log_file) end end diff --git a/lib/imbecile/generator.rb b/lib/imbecile/generator.rb index fa9ec9f..081b32c 100644 --- a/lib/imbecile/generator.rb +++ b/lib/imbecile/generator.rb @@ -3,8 +3,9 @@ module Imbecile # Class to generate the parser generator source. class Generator - def initialize(grammar) + def initialize(grammar, log_file) @grammar = grammar + @log_file = log_file end def generate(output_file)