summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/slop/error.rb5
-rw-r--r--lib/slop/option.rb6
-rw-r--r--lib/slop/parser.rb9
3 files changed, 20 insertions, 0 deletions
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