Skip to content

Commit

Permalink
Merge pull request #218 from woodruffw/required-options
Browse files Browse the repository at this point in the history
Support for required options
  • Loading branch information
olleolleolle authored Oct 5, 2017
2 parents ec47929 + 311cb9f commit 193a826
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Usage
opts = Slop.parse do |o|
o.string '-h', '--host', 'a hostname'
o.integer '--port', 'custom port', default: 80
o.string '-l', '--login', required: true
o.bool '-v', '--verbose', 'enable verbose mode'
o.bool '-q', '--quiet', 'suppress output (quiet mode)'
o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host'
Expand Down Expand Up @@ -197,6 +198,7 @@ Slop will raise errors for the following:

* An option used without an argument when it expects one: `Slop::MissingArgument`
* An option used that Slop doesn't know about: `Slop::UnknownOption`
* An option marked as `required` when not provided: `Slop::MissingRequiredOption`

These errors inherit from `Slop::Error`, so you can rescue them all.
Alternatively you can suppress these errors with the `suppress_errors` config
Expand Down
5 changes: 5 additions & 0 deletions lib/slop/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ def initialize(msg, flag)
@flag = flag
end
end

# Raised when a required option is *not* parsed.
# Suppress with the `suppress_errors` config option.
class MissingRequiredOption < Error
end
end
6 changes: 6 additions & 0 deletions lib/slop/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Option
help: true,
tail: false,
underscore_flags: true,
required: false,
}

# An Array of flags this option matches.
Expand Down Expand Up @@ -101,6 +102,11 @@ def suppress_errors?
config[:suppress_errors]
end

# Returns true if an exception should be raised when this option isn't supplied.
def required?
config[:required]
end

# Returns all flags joined by a comma. Used by the help string.
def flag
flags.join(", ")
Expand Down
9 changes: 9 additions & 0 deletions lib/slop/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def parse(strings)

@arguments += ignored_args

if !suppress_errors?
unused_options.each do |o|
if o.config[:required]
pretty_flags = o.flags.map { |f| "`#{f}'" }.join(", ")
raise MissingRequiredOption, "missing required option #{pretty_flags}"
end
end
end

Result.new(self).tap do |result|
used_options.each { |o| o.finish(result) }
end
Expand Down
14 changes: 14 additions & 0 deletions test/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@
opts.parse %w(--foo)
end
end

describe Slop::MissingRequiredOption do
it "raises when a required option is missing" do
opts = Slop::Options.new
opts.string "-n", "--name", required: true
assert_raises(Slop::MissingRequiredOption) { opts.parse [] }
end

it "does not raise when errors are suppressed" do
opts = Slop::Options.new(suppress_errors: true)
opts.string "-n", "--name", required: true
opts.parse []
end
end

0 comments on commit 193a826

Please sign in to comment.