Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docopt::Exit#error? to infer exit status #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/docopt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ def message
@@message
end

def initialize(message='')
def initialize(message='', error=true)
@@message = (message + "\n" + @@usage).strip
@error = error
end

def error?
!!@error
end
end

Expand Down Expand Up @@ -634,11 +639,11 @@ def dump_patterns(pattern, indent=0)
def extras(help, version, options, doc)
if help and options.any? { |o| ['-h', '--help'].include?(o.name) && o.value }
Exit.set_usage(nil)
raise Exit, doc.strip
raise Exit.new(doc.strip, false)
end
if version and options.any? { |o| o.name == '--version' && o.value }
Exit.set_usage(nil)
raise Exit, version
raise Exit.new(version, false)
end
end

Expand Down
25 changes: 25 additions & 0 deletions test/test_docopt.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'test/unit'
require 'pathname'
require 'docopt'

class DocoptTest < Test::Unit::TestCase

Expand All @@ -9,4 +10,28 @@ def test_docopt_reference_testcases
puts
assert system('python', "test/language_agnostic_tester.py", "test/testee.rb", chdir: TOPDIR)
end

def test_exit_success_on_help
exception = assert_raises(Docopt::Exit) do
Docopt.docopt("Usage: mytool -a", :argv => ["--help"])
end

assert !exception.error?
end

def test_exit_success_on_version
exception = assert_raises(Docopt::Exit) do
Docopt.docopt("Usage: mytool -a", :version => "1.2.3", :argv => ["--version"])
end

assert !exception.error?
end

def test_exit_failure_on_unknown_option
exception = assert_raises(Docopt::Exit) do
Docopt.docopt("Usage: mytool -a", :argv => ["--foo"])
end

assert exception.error?
end
end