Skip to content

Commit

Permalink
proper error handling for unknown options (fix #110)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtsnjp committed Feb 2, 2024
1 parent 1d2ceb6 commit 0a7a7a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
6 changes: 3 additions & 3 deletions script/texdoclib-cli.tlu
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ local function parse_options()
end

for i, o in ipairs(C.options) do
if k == o["short"] or k == o["long"] then
if k == o['short'] or k == o['long'] then
k = i
break
end
end

option = C.options[k]
if option['group'] == 'action' then
if option ~= nil and option['group'] == 'action' then
if option['long'] == 'just-view' then
return true, 'view', cl_config
elseif option['long'] == 'print-completion' then
return true, 'complete', cl_config
else
return true, option['long'], cl_config
end
elseif option['group'] then
elseif option~=nil and option['group'] then
if option['type'] == 'boolean' then
option['action'](cl_config, curr_arg)
elseif option['type'] == 'string' then
Expand Down
28 changes: 0 additions & 28 deletions spec/misc/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,6 @@
RSpec.describe "Errors", :type => :aruba do
include_context "messages"

context "running without any option nor argument" do
before(:each) { run_texdoc }

it 'should result in the "no action" error' do
expect(last_command_started).to have_exit_status(2)
expect(stderr).to include(error_line "No action specified.")
expect(stderr).to include(error_line msg_usage)
end
end

context "missing arguments for Option -d" do
before(:each) { run_texdoc "-d" }

it 'should result in a getopt parser error' do
expect(last_command_started).to have_exit_status(1)
expect(stderr).to include(error_line "Option -d requires an argument.")
end
end

context "missing arguments for Option -c" do
before(:each) { run_texdoc "-c" }

it 'should result in a getopt parser error' do
expect(last_command_started).to have_exit_status(1)
expect(stderr).to include(error_line "Option -c requires an argument.")
end
end

context "when any document for input cannot be found" do
let(:nonexist_pkg) { "never_never_existing_package_foooooooooo" }

Expand Down
38 changes: 38 additions & 0 deletions spec/misc/getopt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,42 @@
debug_line "search", "Searching documents for pattern \"-texlive-en\"")
end
end

# error cases
context "running without any option nor argument" do
before(:each) { run_texdoc }

it 'should result in the "no action" error' do
expect(last_command_started).to have_exit_status(2)
expect(stderr).to include(error_line "No action specified.")
expect(stderr).to include(error_line msg_usage)
end
end

context "missing arguments for Option -d" do
before(:each) { run_texdoc "-d" }

it "should result in a getopt parser error" do
expect(last_command_started).to have_exit_status(1)
expect(stderr).to include(error_line "Option -d requires an argument.")
end
end

context "missing arguments for Option -c" do
before(:each) { run_texdoc "-c" }

it "should result in a getopt parser error" do
expect(last_command_started).to have_exit_status(1)
expect(stderr).to include(error_line "Option -c requires an argument.")
end
end

context "given an unknown option" do
before(:each) { run_texdoc "-x" }

it "should result in a getopt parser error" do
expect(last_command_started).to have_exit_status(2)
expect(stderr).to include(error_line "unknown option: -x")
end
end
end

0 comments on commit 0a7a7a6

Please sign in to comment.