Sha256: 537bdb964f7c01a2d3cc48f81191110832ddeaf7928749ed2d3149cb2fcec651

Contents?: true

Size: 1.19 KB

Versions: 13

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8

module Nake
  module ArgvParser
    def self.extract!(args)
      args.dup.inject(Hash.new) do |options, argument|
        key, value = self.parse(argument)
        if key
          options[key] = value
          args.delete(argument)
        end
        options
      end
    end

    def self.extract(args)
      args.inject(Hash.new) do |options, argument|
        key, value = self.parse(argument)
        if key
          options[key] = value
        end
        options
      end
    end

    def self.parse!(argument)
      self.parse(argument) || raise("Argument #{argument} can't be parsed!")
    end

    def self.parse(argument)
      case argument
      when /^--no-([^=]+)$/ # --no-git-repository
        return [$1.gsub("-", "_").to_sym, false]
      when /^--([^=]+)$/    # --git-repository
        return [$1.gsub("-", "_").to_sym, true]
      when /^--([^=]+)=([^,]+)$/ # --controller=post
        key, value = $1, $2
        return [key.gsub("-", "_").to_sym, value.dup]
      when /^--([^=]+)=(.+)$/    # --controllers=posts,comments
        key, value = $1, $2
        return [key.gsub("-", "_").to_sym, value.split(",")]
      else
        return false
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
nake-0.1.1 lib/nake/argv.rb
nake-0.1 lib/nake/argv.rb
nake-0.0.9.5 lib/nake/argv.rb
nake-0.0.9.4 lib/nake/argv.rb
nake-0.0.9.3 lib/nake/argv.rb
nake-0.0.9.2 lib/nake/argv.rb
nake-0.0.9.pre lib/nake/argv.rb
do_riak-0.10.1.pre gems/gems/nake-0.0.8/lib/nake/argv.rb
nake-0.0.8.pre lib/nake/argv.rb
nake-0.0.8 lib/nake/argv.rb
nake-0.0.7 lib/nake/argv.rb
nake-0.0.6 lib/nake/argv.rb
nake-0.0.5 lib/nake/argv.rb