Skip to content

Commit 647aa8a

Browse files
committed
Update cli_commands to add help and description.
1 parent e3ce35f commit 647aa8a

File tree

2 files changed

+117
-97
lines changed

2 files changed

+117
-97
lines changed

lib/json/ld/format.rb

Lines changed: 109 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -47,123 +47,143 @@ def self.detect(sample)
4747
# @return [Hash{Symbol => Lambda(Array, Hash)}]
4848
def self.cli_commands
4949
{
50-
expand: ->(files, options) do
51-
out = options[:output] || $stdout
52-
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
53-
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
54-
if options[:format] == :jsonld
55-
if files.empty?
56-
# If files are empty, either use options[:execute]
57-
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
58-
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
59-
JSON::LD::API.expand(input, options) do |expanded|
60-
out.puts expanded.to_json(JSON::LD::JSON_STATE)
50+
expand: {
51+
description: "Expand JSON-LD or parsed RDF",
52+
parse: false,
53+
help: "expand [--context <context-file>] files ...",
54+
lambda: ->(files, options) do
55+
out = options[:output] || $stdout
56+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
57+
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
58+
if options[:format] == :jsonld
59+
if files.empty?
60+
# If files are empty, either use options[:execute]
61+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
62+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
63+
JSON::LD::API.expand(input, options) do |expanded|
64+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
65+
end
66+
else
67+
files.each do |file|
68+
JSON::LD::API.expand(file, options) do |expanded|
69+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
70+
end
71+
end
6172
end
6273
else
63-
files.each do |file|
64-
JSON::LD::API.expand(file, options) do |expanded|
74+
# Turn RDF into JSON-LD first
75+
RDF::CLI.parse(files, options) do |reader|
76+
JSON::LD::API.fromRdf(reader) do |expanded|
6577
out.puts expanded.to_json(JSON::LD::JSON_STATE)
6678
end
6779
end
6880
end
69-
else
70-
# Turn RDF into JSON-LD first
71-
RDF::CLI.parse(files, options) do |reader|
72-
JSON::LD::API.fromRdf(reader) do |expanded|
73-
out.puts expanded.to_json(JSON::LD::JSON_STATE)
74-
end
75-
end
7681
end
77-
end,
78-
compact: ->(files, options) do
79-
raise ArgumentError, "Compacting requires a context" unless options[:context]
80-
out = options[:output] || $stdout
81-
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
82-
if options[:format] == :jsonld
83-
if files.empty?
84-
# If files are empty, either use options[:execute]
85-
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
86-
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
87-
JSON::LD::API.compact(input, options[:context], options) do |compacted|
88-
out.puts compacted.to_json(JSON::LD::JSON_STATE)
89-
end
90-
else
91-
files.each do |file|
92-
JSON::LD::API.compact(file, options[:context], options) do |compacted|
82+
},
83+
compact: {
84+
description: "Compact JSON-LD or parsed RDF",
85+
parse: false,
86+
help: "compact --context <context-file> files ...",
87+
lambda: ->(files, options) do
88+
raise ArgumentError, "Compacting requires a context" unless options[:context]
89+
out = options[:output] || $stdout
90+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
91+
if options[:format] == :jsonld
92+
if files.empty?
93+
# If files are empty, either use options[:execute]
94+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
95+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
96+
JSON::LD::API.compact(input, options[:context], options) do |compacted|
9397
out.puts compacted.to_json(JSON::LD::JSON_STATE)
9498
end
99+
else
100+
files.each do |file|
101+
JSON::LD::API.compact(file, options[:context], options) do |compacted|
102+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
103+
end
104+
end
95105
end
96-
end
97-
else
98-
# Turn RDF into JSON-LD first
99-
RDF::CLI.parse(files, options) do |reader|
100-
JSON::LD::API.fromRdf(reader) do |expanded|
101-
JSON::LD::API.compact(expanded, options[:context], options) do |compacted|
102-
out.puts compacted.to_json(JSON::LD::JSON_STATE)
106+
else
107+
# Turn RDF into JSON-LD first
108+
RDF::CLI.parse(files, options) do |reader|
109+
JSON::LD::API.fromRdf(reader) do |expanded|
110+
JSON::LD::API.compact(expanded, options[:context], options) do |compacted|
111+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
112+
end
103113
end
104114
end
105115
end
106116
end
107-
end,
108-
flatten: ->(files, options) do
109-
out = options[:output] || $stdout
110-
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
111-
if options[:format] == :jsonld
112-
if files.empty?
113-
# If files are empty, either use options[:execute]
114-
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
115-
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
116-
JSON::LD::API.flatten(input, options[:context], options) do |flattened|
117-
out.puts flattened.to_json(JSON::LD::JSON_STATE)
118-
end
119-
else
120-
files.each do |file|
121-
JSON::LD::API.flatten(file, options[:context], options) do |flattened|
117+
},
118+
flatten: {
119+
description: "Flatten JSON-LD or parsed RDF",
120+
parse: false,
121+
help: "flatten [--context <context-file>] files ...",
122+
lambda: ->(files, options) do
123+
out = options[:output] || $stdout
124+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
125+
if options[:format] == :jsonld
126+
if files.empty?
127+
# If files are empty, either use options[:execute]
128+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
129+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
130+
JSON::LD::API.flatten(input, options[:context], options) do |flattened|
122131
out.puts flattened.to_json(JSON::LD::JSON_STATE)
123132
end
133+
else
134+
files.each do |file|
135+
JSON::LD::API.flatten(file, options[:context], options) do |flattened|
136+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
137+
end
138+
end
124139
end
125-
end
126-
else
127-
# Turn RDF into JSON-LD first
128-
RDF::CLI.parse(files, options) do |reader|
129-
JSON::LD::API.fromRdf(reader) do |expanded|
130-
JSON::LD::API.flatten(expanded, options[:context], options) do |flattened|
131-
out.puts flattened.to_json(JSON::LD::JSON_STATE)
140+
else
141+
# Turn RDF into JSON-LD first
142+
RDF::CLI.parse(files, options) do |reader|
143+
JSON::LD::API.fromRdf(reader) do |expanded|
144+
JSON::LD::API.flatten(expanded, options[:context], options) do |flattened|
145+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
146+
end
132147
end
133148
end
134149
end
135150
end
136-
end,
137-
frame: ->(files, options) do
138-
raise ArgumentError, "Framing requires a frame" unless options[:frame]
139-
out = options[:output] || $stdout
140-
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
141-
if options[:format] == :jsonld
142-
if files.empty?
143-
# If files are empty, either use options[:execute]
144-
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
145-
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
146-
JSON::LD::API.frame(input, options[:frame], options) do |framed|
147-
out.puts framed.to_json(JSON::LD::JSON_STATE)
148-
end
149-
else
150-
files.each do |file|
151-
JSON::LD::API.frame(file, options[:frame], options) do |framed|
151+
},
152+
frame: {
153+
description: "Flatten JSON-LD or parsed RDF",
154+
parse: false,
155+
help: "flatten --frame <frame-file> files ...",
156+
lambda: ->(files, options) do
157+
raise ArgumentError, "Framing requires a frame" unless options[:frame]
158+
out = options[:output] || $stdout
159+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
160+
if options[:format] == :jsonld
161+
if files.empty?
162+
# If files are empty, either use options[:execute]
163+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
164+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
165+
JSON::LD::API.frame(input, options[:frame], options) do |framed|
152166
out.puts framed.to_json(JSON::LD::JSON_STATE)
153167
end
168+
else
169+
files.each do |file|
170+
JSON::LD::API.frame(file, options[:frame], options) do |framed|
171+
out.puts framed.to_json(JSON::LD::JSON_STATE)
172+
end
173+
end
154174
end
155-
end
156-
else
157-
# Turn RDF into JSON-LD first
158-
RDF::CLI.parse(files, options) do |reader|
159-
JSON::LD::API.fromRdf(reader) do |expanded|
160-
JSON::LD::API.frame(expanded, options[:frame], options) do |framed|
161-
out.puts framed.to_json(JSON::LD::JSON_STATE)
175+
else
176+
# Turn RDF into JSON-LD first
177+
RDF::CLI.parse(files, options) do |reader|
178+
JSON::LD::API.fromRdf(reader) do |expanded|
179+
JSON::LD::API.frame(expanded, options[:frame], options) do |framed|
180+
out.puts framed.to_json(JSON::LD::JSON_STATE)
181+
end
162182
end
163183
end
164184
end
165185
end
166-
end,
186+
},
167187
}
168188
end
169189

spec/format_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,37 @@
7575

7676
describe "#expand" do
7777
it "expands RDF" do
78-
expect {RDF::CLI.exec_command("expand", [ttl], format: :ttl)}.to write.to(:output)
78+
expect {RDF::CLI.exec(["expand", ttl], format: :ttl)}.to write.to(:output)
7979
end
8080
it "expands JSON" do
81-
expect {RDF::CLI.exec_command("expand", [json], format: :jsonld)}.to write.to(:output)
81+
expect {RDF::CLI.exec(["expand", json], format: :jsonld)}.to write.to(:output)
8282
end
8383
end
8484

8585
describe "#compact" do
8686
it "compacts RDF" do
87-
expect {RDF::CLI.exec_command("compact", [ttl], context: context, format: :ttl)}.to write.to(:output)
87+
expect {RDF::CLI.exec(["compact", ttl], context: context, format: :ttl)}.to write.to(:output)
8888
end
8989
it "compacts JSON" do
90-
expect {RDF::CLI.exec_command("compact", [json], context: context, format: :jsonld)}.to write.to(:output)
90+
expect {RDF::CLI.exec(["compact", json], context: context, format: :jsonld)}.to write.to(:output)
9191
end
9292
end
9393

9494
describe "#flatten" do
9595
it "flattens RDF" do
96-
expect {RDF::CLI.exec_command("flatten", [ttl], context: context, format: :ttl)}.to write.to(:output)
96+
expect {RDF::CLI.exec(["flatten", ttl], context: context, format: :ttl)}.to write.to(:output)
9797
end
9898
it "flattens JSON" do
99-
expect {RDF::CLI.exec_command("flatten", [json], context: context, format: :jsonld)}.to write.to(:output)
99+
expect {RDF::CLI.exec(["flatten", json], context: context, format: :jsonld)}.to write.to(:output)
100100
end
101101
end
102102

103103
describe "#frame" do
104104
it "frames RDF" do
105-
expect {RDF::CLI.exec_command("frame", [ttl], frame: context, format: :ttl)}.to write.to(:output)
105+
expect {RDF::CLI.exec(["frame", ttl], frame: context, format: :ttl)}.to write.to(:output)
106106
end
107107
it "frames JSON" do
108-
expect {RDF::CLI.exec_command("frame", [json], frame: context, format: :jsonld)}.to write.to(:output)
108+
expect {RDF::CLI.exec(["frame", json], frame: context, format: :jsonld)}.to write.to(:output)
109109
end
110110
end
111111
end

0 commit comments

Comments
 (0)