Skip to content

Commit 5df84c6

Browse files
committed
Some CLI fixes.
1 parent 8603ec8 commit 5df84c6

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

lib/rdf/cli.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,10 @@ def self.exec(args, output: $stdout, option_parser: nil, messages: {}, **options
506506
options[:output_format] = options[:output_format].to_sym if options[:output_format]
507507

508508
# Allow repository to be set via option.
509-
@repository = options.fetch(:repository) {
510-
options[:ordered] ?
509+
@repository = options[:repository] ||
510+
(options[:ordered] ?
511511
[].extend(RDF::Enumerable, RDF::Queryable) :
512-
RDF::Repository.new
513-
}
512+
RDF::Repository.new)
514513

515514
# Parse input files if any command requires it
516515
if cmds.any? {|c| COMMANDS[c.to_sym][:parse]}
@@ -528,8 +527,7 @@ def self.exec(args, output: $stdout, option_parser: nil, messages: {}, **options
528527
COMMANDS[command.to_sym][:lambda].call(args,
529528
output: output,
530529
messages: messages,
531-
repository: repository,
532-
**options)
530+
**options.merge(repository: repository))
533531
end
534532

535533
# Normalize messages

spec/cli_spec.rb

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,37 @@
123123
end
124124

125125
describe ".add_command" do
126-
it "adds a command" do
126+
after {RDF::CLI::COMMANDS.delete(:foo)}
127+
128+
it "adds a command with block" do
127129
RDF::CLI.add_command(:foo) do |argv, opts|
128130
$stdout.puts "Hello, World!"
129131
end
130132
expect {RDF::CLI.exec(["foo"])}.to write("Hello, World!").to(:output)
131133
end
134+
135+
it "adds a command with lambda option" do
136+
lambda = ->(argv, opts) do
137+
$stdout.puts "Hello, World!"
138+
end
139+
RDF::CLI.add_command(:foo, lambda: lambda)
140+
expect {RDF::CLI.exec(["foo"])}.to write("Hello, World!").to(:output)
141+
end
142+
143+
it "calls command with repository" do
144+
RDF::CLI.add_command(:foo) do |argv, opts|
145+
expect(opts).to include(repository: kind_of(RDF::Enumerable))
146+
end
147+
RDF::CLI.exec(["foo"])
148+
end
149+
150+
it "calls command with specified repository" do
151+
repo = double(:repo)
152+
RDF::CLI.add_command(:foo, repository: repo) do |argv, opts|
153+
expect(opts).to include(repository: repo)
154+
end
155+
RDF::CLI.exec(["foo"])
156+
end
132157
end
133158

134159
context "commands" do
@@ -215,12 +240,16 @@
215240
end
216241

217242
it "complains if filtered command is attempted" do
218-
RDF::CLI.add_command(:foo, filter: {output_format: :nquads})
243+
RDF::CLI.add_command(:filtered, filter: {output_format: :nquads})
219244
expect do
220245
expect do
221-
RDF::CLI.exec(["foo"], output_format: :ntriples)
246+
RDF::CLI.exec(["filtered"], output_format: :ntriples)
222247
end.to raise_error(ArgumentError)
223-
end.to write(%(Command "foo" requires output_format: nquads, not ntriples)).to(:output)
248+
end.to write(%(Command "filtered" requires output_format: nquads, not ntriples)).to(:output)
249+
end
250+
251+
it "uses repository specified in command" do
252+
RDF::CLI.add_command(:with_repo, repository: double(:repo))
224253
end
225254

226255
context "chaining" do

0 commit comments

Comments
 (0)