Skip to content

Commit 42b0cb6

Browse files
committed
Fix option handling in jsonld CLI.
1 parent d864b20 commit 42b0cb6

File tree

3 files changed

+2896
-30
lines changed

3 files changed

+2896
-30
lines changed

bin/jsonld

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require 'getoptlong'
1010
require 'open-uri'
1111
require 'logger'
1212

13-
def run(input, options)
13+
def run(input, options, parser_options)
1414
reader_class = RDF::Reader.for(options[:input_format].to_sym)
1515
raise "Reader not found for #{options[:input_format]}" unless reader_class
1616

@@ -19,38 +19,38 @@ def run(input, options)
1919

2020
# If input format is not JSON-LD, transform input to JSON-LD first
2121
reader = if options[:input_format] != :jsonld
22-
reader_class.new(input, options[:parser_options])
22+
reader_class.new(input, parser_options)
2323
end
2424

2525
start = Time.new
2626
if options[:expand]
27-
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
27+
parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.has_key?(:context)
2828
input = JSON::LD::API.fromRdf(reader) if reader
29-
output = JSON::LD::API.expand(input, options)
29+
output = JSON::LD::API.expand(input, parser_options)
3030
secs = Time.new - start
3131
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
3232
STDERR.puts "Expanded in #{secs} seconds." unless options[:quiet]
3333
elsif options[:compact]
3434
input = JSON::LD::API.fromRdf(reader) if reader
35-
output = JSON::LD::API.compact(input, options[:context], options)
35+
output = JSON::LD::API.compact(input, parser_options[:context], parser_options)
3636
secs = Time.new - start
3737
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
3838
STDERR.puts "Compacted in #{secs} seconds." unless options[:quiet]
3939
elsif options[:flatten]
4040
input = JSON::LD::API.fromRdf(reader) if reader
41-
output = JSON::LD::API.flatten(input, options[:context], options)
41+
output = JSON::LD::API.flatten(input, parser_options[:context], parser_options)
4242
secs = Time.new - start
4343
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
4444
STDERR.puts "Flattened in #{secs} seconds." unless options[:quiet]
4545
elsif options[:frame]
4646
input = JSON::LD::API.fromRdf(reader) if reader
47-
output = JSON::LD::API.frame(input, options[:frame], options)
47+
output = JSON::LD::API.frame(input, parser_options[:frame], parser_options)
4848
secs = Time.new - start
4949
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
5050
STDERR.puts "Framed in #{secs} seconds." unless options[:quiet]
5151
else
52-
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
53-
parser_options = options[:parser_options].merge(standard_prefixes: true)
52+
parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.has_key?(:context)
53+
parser_options[:standard_prefixes] = true
5454
reader ||= JSON::LD::Reader.new(input, parser_options)
5555
num = 0
5656
RDF::Writer.for(options[:output_format]).new(options[:output], parser_options) do |w|
@@ -86,7 +86,6 @@ parser_options = {
8686
}
8787

8888
options = {
89-
parser_options: parser_options,
9089
output: STDOUT,
9190
output_format: :jsonld,
9291
input_format: :jsonld,
@@ -141,49 +140,47 @@ opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
141140
opts.each do |opt, arg|
142141
case opt
143142
when '--debug' then logger.level = Logger::DEBUG
144-
when '--compact' then options[:compact] = true
145-
when "--compactArrays" then options[:compactArrays] = (arg || 'true') == 'true'
146-
when '--context' then options[:context] = arg
143+
when '--compact' then parser_options[:compact] = true
144+
when "--compactArrays" then parser_options[:compactArrays] = (arg || 'true') == 'true'
145+
when '--context' then parser_options[:context] = RDF::URI(arg).absolute? ? arg : File.open(arg)
147146
when '--evaluate' then input = arg
148147
when '--expand' then options[:expand] = true
149-
when "--expanded" then options[:expanded] = (arg || 'true') == 'true'
150-
when "--explicit" then options[:compactArrays] = (arg || 'true') == 'true'
148+
when "--expanded" then parser_options[:expanded] = (arg || 'true') == 'true'
149+
when "--explicit" then parser_options[:compactArrays] = (arg || 'true') == 'true'
151150
when '--format' then options[:output_format] = arg.to_sym
152151
when '--flatten' then options[:flatten] = arg
153-
when '--frame' then options[:frame] = arg
152+
when '--frame' then options[:frame] = parser_otpions[:frame] = RDF::URI(arg).absolute? ? arg : File.open(arg)
154153
when '--input-format' then options[:input_format] = arg.to_sym
155-
when "--omitDefault" then options[:omitDefault] = (arg || 'true') == 'true'
154+
when "--omitDefault" then parser_options[:omitDefault] = (arg || 'true') == 'true'
156155
when '--output' then options[:output] = File.open(arg, "w")
157156
when '--parse-only' then options[:parse_only] = true
158-
when '--processingMode' then options[:processingMode] = arg
157+
when '--processingMode' then parser_options[:processingMode] = arg
159158
when '--quiet'
160159
options[:quiet] = true
161160
logger.level = Logger::FATAL
162-
when "--rename_bnodes" then options[:rename_bnodes] = (arg || 'true') == 'true'
163-
when "--requireAll" then options[:requireAll] = (arg || 'true') == 'true'
161+
when "--rename_bnodes" then parser_options[:rename_bnodes] = (arg || 'true') == 'true'
162+
when "--requireAll" then parser_options[:requireAll] = (arg || 'true') == 'true'
164163
when '--stream' then parser_options[:stream] = true
165-
when "--unique_bnodes" then options[:unique_bnodes] = (arg || 'true') == 'true'
164+
when "--unique_bnodes" then parser_options[:unique_bnodes] = (arg || 'true') == 'true'
166165
when '--uri' then parser_options[:base] = arg
167166
when '--validate' then parser_options[:validate] = true
168167
when '--help' then usage
169168
when '--embed'
170169
case arg
171-
when '@always', '@never', '@link', '@last', '@first'
172-
options[:embed] = arg
170+
when '@always', '@never', '@link', '@once'
171+
parser_options[:embed] = arg
173172
when 'true'
174-
options[:embed] = '@never'
173+
parser_options[:embed] = '@never'
175174
when 'false'
176-
options[:embed] = '@first'
175+
parser_options[:embed] = '@first'
177176
else
178-
STDERR.puts "--embed option takes one of @always, @never, @link, @first, or @last"
177+
STDERR.puts "--embed option takes one of @always, @never, @link, or @once"
179178
exit(1)
180179
end
181180
end
182181
end
183182

184183
# Hack
185-
options[:parser_options][:context] = options[:context] if parser_options[:stream]
186-
187184
if !(options.keys & [:expand, :compact, :flatten, :frame]).empty? &&
188185
(parser_options[:stream] || options[:output_format] != :jsonld)
189186
STDERR.puts "Incompatible options"
@@ -192,11 +189,11 @@ end
192189

193190
if ARGV.empty?
194191
s = input ? input : $stdin.read
195-
run(StringIO.new(s), options)
192+
run(StringIO.new(s), options, parser_options)
196193
else
197194
ARGV.each do |file|
198195
# Call with opened files
199-
RDF::Util::File.open_file(file, options) {|f| run(f, options)}
196+
RDF::Util::File.open_file(file, options) {|f| run(f, options, parser_options)}
200197
end
201198
end
202199
puts
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"@context": {
3+
"ns": "http://example.org/ns/",
4+
"xsd": "http://www.w3.org/2001/XMLSchema#",
5+
"s": "http://schema.org/",
6+
7+
"id": "@id",
8+
"actor": {
9+
"@id": "s:actor",
10+
"@context": {
11+
"id": null,
12+
"url": "@id",
13+
"login": "s:accountId",
14+
"avatar_url": "s:image"
15+
}
16+
},
17+
"repo": {
18+
"@id": "ns:onRepository",
19+
"@context": {
20+
"id": null,
21+
"url": "@id",
22+
"name": "s:name"
23+
}
24+
},
25+
"created_at": {
26+
"@id": "s:startDate",
27+
"@type": "xsd:dateTimeStamp"
28+
},
29+
"payload": "@nest",
30+
"issue": {
31+
"@id": "ns:issue",
32+
"@context": {
33+
"id": null,
34+
"url": "@id",
35+
"title": "s:description",
36+
"user": {
37+
"@id": "s:creator",
38+
"@context": {
39+
"login": "s:accountId",
40+
"avatar_url": "s:image"
41+
}
42+
}
43+
}
44+
},
45+
"IssueCommentEvent": "ns:IssueComment"
46+
}
47+
}

0 commit comments

Comments
 (0)