Sha256: 6d46e360bb622f7e11085003127fbec79ef112987bfd07aea0f472e3268a5080

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Shoe
  module Extensions

    # This extension allows for default options in OptionParser, processing
    # them before any given options and appending them nicely to the help
    # message. (It also does the conventional thing and errors with usage when
    # there's trouble parsing options.)
    #
    # Note that it's important to use #order rather than #permute, to ensure
    # that the given options have a chance to override the defaults. (These are
    # <tt>POSIX</tt>-ly correct semantics.)
    #
    # To use:
    #   parser = OptionsParser.new do |opts|
    #              opts.extend(Shoe::Extensions::OptionParser)
    #              opts.defaults = %w(--foo --no-bar --baz=5)
    #              # ... and so on ...
    #            end
    #
    #   parser.order(ARGV)
    module OptionParser
      attr_reader :defaults

      def defaults=(defaults)
        @defaults = defaults.extend(Defaults)
      end

      def order(*args, &block)
        begin
          super(*defaults.followed_by(*args), &block)
        rescue ::OptionParser::ParseError
          abort($!)
        end
      end

      def help
        defaults.help(super, summary_indent)
      end

      private

      module Defaults #:nodoc:
        def followed_by(*args)
          dup.concat(*args)
        end

        def help(before, indent)
          "#{before}\nDefaults:\n#{indent}#{join ' '}"
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoe-0.6.1 lib/shoe/extensions/option_parser.rb