Sha256: d8a0a0b6d0454761956dc2a02f67834c48ad60e4f4b09a7e2a3c7316248160ac

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

require "thor"
require "term/ansicolor"

# Monkey patch Thor so we can print out a warning when there are unknown options, rather than raising an error.
# If PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true, raise the error, as the user will have opted in to this behaviour.
# This is for backwards compatibility reasons, and in the next major version, the flag will be removed.

class Thor
  class Options < Arguments

    alias_method :original_check_unknown!, :check_unknown!

    # Replace the original check_unknown! method with an implementation
    # that will print a warning rather than raising an error
    # unless PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true is set.
    def check_unknown!
      if raise_error_on_unknown_options?
        original_check_unknown!
      else
        check_unknown_and_warn
      end
    end

    def raise_error_on_unknown_options?
      ENV["PACT_BROKER_ERROR_ON_UNKNOWN_OPTION"] == "true"
    end

    def check_unknown_and_warn
      begin
        original_check_unknown!
      rescue UnknownArgumentError => e
        $stderr.puts(::Term::ANSIColor.yellow(e.message))
        $stderr.puts(::Term::ANSIColor.yellow("This is a warning rather than an error so as not to break backwards compatibility. To raise an error for unknown options set PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true"))
        $stderr.puts("\n")
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pact_broker-client-1.77.0 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.76.2 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.76.1 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.76.0 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.75.4 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.75.3 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.75.1 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.75.0 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.74.0 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
pact_broker-client-1.73.0 lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb