Skip to content

Commit

Permalink
add support for +help
Browse files Browse the repository at this point in the history
part of #26
  • Loading branch information
rfdonnelly committed Mar 19, 2016
1 parent 13c4ef4 commit c42761b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bin/jobrnr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require_relative '../lib/jobrnr'

begin
exit Jobrnr::Application.new(ARGV).run
rescue Jobrnr::HelpException => e
puts e.message
exit 0
rescue Jobrnr::Error => e
Jobrnr::Log.error e
end
2 changes: 2 additions & 0 deletions lib/jobrnr/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class OptionsError < Error; end
class ImportError < Error; end
class ArgumentError < Error; end
class TypeError < Error; end

class HelpException < StandardError; end
end
2 changes: 2 additions & 0 deletions lib/jobrnr/plus_option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def parse(specs, plus_option_strings)
option_definitions = transform_specs(specs.clone)
plus_options = plus_options_to_hash(plus_option_strings)

raise Jobrnr::HelpException, help(option_definitions) if plus_options.keys.include?(:help)

unless Jobrnr::Util.array_subset_of?(plus_options.keys, option_definitions.keys)
raise Jobrnr::ArgumentError, "The following options are not valid options: #{unsupported_options(plus_options, option_definitions)}\n\n#{help(option_definitions)}"
end
Expand Down
20 changes: 20 additions & 0 deletions test/plus_option_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@
))
assert_equal(exp, act)
end

it 'supports +help' do
e = assert_raises(Jobrnr::HelpException) { @obj.parse(@specs, %w(+help)) }

assert_equal(Jobrnr::Util.strip_heredoc(<<-EOF).strip, e.message)
OPTIONS
+default-true[=<value>]
An option with a default true value. Default: true
+default-inferred[=<value>]
An option with an inferred default value. Default: false
+fix-num=<value>
A fixnum option. Default: 1
+string=<value>
A string option. Default: hello world
EOF
end
end

describe 'errors' do
Expand Down

0 comments on commit c42761b

Please sign in to comment.