CLI: accept --log option

This commit is contained in:
Josh Holtrop 2021-06-19 12:06:02 -04:00
parent d2fac07249
commit d552f2a540
3 changed files with 21 additions and 10 deletions

View File

@ -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

View File

@ -4,6 +4,7 @@ module Imbecile
USAGE = <<EOF
Usage: #{$0} [options] <input-file> <output-file>
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

View File

@ -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)