From 311cb9fbe94fe09c0dc08a52f5bff169529960ba Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 29 Sep 2017 18:31:28 -0400 Subject: Support for required options This commit introduces support for required options, which are options that cause the parser to raise a `MissingRequiredOption` exception if not present. Options can be marked as required by passing `required: true` in their configuration, and any errors caused by missing required options can be suppressed via `suppress_errors: true`. --- README.md | 2 ++ lib/slop/error.rb | 5 +++++ lib/slop/option.rb | 6 ++++++ lib/slop/parser.rb | 9 +++++++++ test/error_test.rb | 14 ++++++++++++++ 5 files changed, 36 insertions(+) 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 -- cgit v1.2.1