summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlle Jonsson <olle.jonsson@gmail.com>2017-10-05 23:07:55 +0200
committerGitHub <noreply@github.com>2017-10-05 23:07:55 +0200
commit193a8263a34820003a45ad620518fa8df0454b91 (patch)
tree8eb7c8bcba2f7210e47b393dd200c2423dab82c0
parentec47929b2ffe986b13701c30f1bbe3018593bc6c (diff)
parent311cb9fbe94fe09c0dc08a52f5bff169529960ba (diff)
downloadslop-193a8263a34820003a45ad620518fa8df0454b91.tar.gz
Merge pull request #218 from woodruffw/required-options
Support for required options
-rw-r--r--README.md2
-rw-r--r--lib/slop/error.rb5
-rw-r--r--lib/slop/option.rb6
-rw-r--r--lib/slop/parser.rb9
-rw-r--r--test/error_test.rb14
5 files changed, 36 insertions, 0 deletions
diff --git a/README.md b/README.md
index 505d4c5..4d686ce 100644
--- a/README.md
+++ b/README.md
@@ -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'
@@ -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
diff --git a/lib/slop/error.rb b/lib/slop/error.rb
index b02de3b..e024645 100644
--- a/lib/slop/error.rb
+++ b/lib/slop/error.rb
@@ -32,4 +32,9 @@ module Slop
@flag = flag
end
end
+
+ # Raised when a required option is *not* parsed.
+ # Suppress with the `suppress_errors` config option.
+ class MissingRequiredOption < Error
+ end
end
diff --git a/lib/slop/option.rb b/lib/slop/option.rb
index b8533ae..093b37f 100644
--- a/lib/slop/option.rb
+++ b/lib/slop/option.rb
@@ -4,6 +4,7 @@ module Slop
help: true,
tail: false,
underscore_flags: true,
+ required: false,
}
# An Array of flags this option matches.
@@ -101,6 +102,11 @@ module Slop
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(", ")
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb
index 3cba82b..463e75a 100644
--- a/lib/slop/parser.rb
+++ b/lib/slop/parser.rb
@@ -80,6 +80,15 @@ module Slop
@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
diff --git a/test/error_test.rb b/test/error_test.rb
index 2f7730b..d9c71ab 100644
--- a/test/error_test.rb
+++ b/test/error_test.rb
@@ -49,3 +49,17 @@ describe Slop::UnknownOption do
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