CLI: accept --log option
This commit is contained in:
parent
d2fac07249
commit
d552f2a540
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user