Skip to content

Commit 48f164c

Browse files
committed
saner option parsing
1 parent 6639797 commit 48f164c

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
./ruby pg_logger.rb -t -f $1

‎v4-cokapi/backends/ruby/golden_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
INPUT_FILE_EXTENSION = '.rb'
99

1010
# program to run, with input file as an extra argument
11-
PROGRAM = ['./ruby', 'pg_logger.rb', 'stdout']
11+
PROGRAM = ['./ruby', 'pg_logger.rb', '-f']
1212

1313
# this program should output to stdout, which will be redirected to this
1414
# extension:

‎v4-cokapi/backends/ruby/pg_logger.rb‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,37 @@
5454

5555

5656
require 'json'
57+
require 'optparse'
5758
require 'set'
5859
require 'stringio'
5960

6061
require 'debug_inspector' # gem install debug_inspector, use on Ruby 2.X
6162

62-
#require 'binding_of_caller' # gem install binding_of_caller
63+
trace_output_fn = 'stdout' # default
64+
script_name = nil
65+
cod = nil
6366

67+
OptionParser.new do |opts|
68+
opts.banner = "Usage: ./ruby pg_logger.rb [options]"
6469

65-
# pass 'stdout' to print trace to stdout
66-
# pass 'jstrace' to print a JS trace to test-trace.js
67-
trace_output_fn = ARGV[0]
68-
trace_output_fn = "../../../v3/test-trace.js" if trace_output_fn == 'jstrace'
70+
opts.on("-t", "--tracefile", "Create a test-trace.js trace file") do |t|
71+
trace_output_fn = "../../../v3/test-trace.js" if t
72+
end
73+
74+
opts.on("-f", "--file FILE", "Execute a .rb file") do |s|
75+
script_name = s
76+
end
6977

70-
script_name = ARGV[1]
71-
cod = File.open(script_name).read
78+
opts.on("-c", "--code CODE", String, "Execute Ruby code from string") do |c|
79+
cod = c
80+
end
81+
82+
end.parse!
83+
84+
# -c gets precedence over -f
85+
if !cod
86+
cod = File.open(script_name).read
87+
end
7288

7389
cur_trace = []
7490
res = {'code' => cod.dup, # make a snapshot of the code NOW before it's modified

0 commit comments

Comments
 (0)