Sha256: 92da374fe83d9c1e974b805bcc2ea1d43af867275ca535b5a49b4152afa33ae7

Contents?: true

Size: 1.05 KB

Versions: 14

Compression:

Stored size: 1.05 KB

Contents

require 'optparse'

module Options

  # Returns a list of options unknown to a particular options parser
  # For example, if '-a' is a known option but '-b' and '-c' are not,
  # unknown_options(parser, ['-a', '-b', '-c']) returns ['-b', '-c'].
  # It also preserves arguments, so 
  # unknown_options(parser, ['-a', '-b', '-c', 'foo bar']) returns 
  # ['-b', '-c', 'foo bar'].
  def self.unknown_options(parser, args)
    unknown = []                                     
    recursive_parse = Proc.new do |arg_list|                   
      begin
        parser.parse!(arg_list)
      rescue OptionParser::InvalidOption => e
        unknown.concat(e.args)
        while !arg_list.empty? && arg_list.first[0] != "-"
          unknown << arg_list.shift
        end
        recursive_parse.call(arg_list)
      end
    end
    recursive_parse.call(args.dup)
    unknown
  end

  # Returns a list of options with unknown options removed
  def self.known_options(parser, args)
    unknown = unknown_options(parser, args)
    args.reject { |arg| unknown.include?(arg) }
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
pivotal-github-0.6.6 lib/pivotal-github/options.rb
pivotal-github-0.6.5 lib/pivotal-github/options.rb
pivotal-github-0.6.4 lib/pivotal-github/options.rb
pivotal-github-0.6.3 lib/pivotal-github/options.rb
pivotal-github-0.6.2 lib/pivotal-github/options.rb
pivotal-github-0.6.1 lib/pivotal-github/options.rb
pivotal-github-0.6.0 lib/pivotal-github/options.rb
pivotal-github-0.5.6 lib/pivotal-github/options.rb
pivotal-github-0.5.5 lib/pivotal-github/options.rb
pivotal-github-0.5.4 lib/pivotal-github/options.rb
pivotal-github-0.5.3 lib/pivotal-github/options.rb
pivotal-github-0.5.2 lib/pivotal-github/options.rb
pivotal-github-0.5.1 lib/pivotal-github/options.rb
pivotal-github-0.5.0 lib/pivotal-github/options.rb