Sha256: ea9c0df4672cc8f751316be07aac42076db004c0235bb68bc3902d70c087e106

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Ratch

  # No fuss access to ARGV. This shows up as #argv in the ratch api.
  #
  # Ratch uses '=' for parameterized flags b/c this make parsing stupid simple
  # and that's a good thing!!!

  module ArgvUtils

    def argv
      @argv ||= (ARGV.dup).extend(Ext)
    end

    module Ext

      def flags
        @flags ||= collect{ |e| e =~ /^-/ && e !~ /=/ }
      end

      def arguments
        @arguments ||= collect{ |e| e !~ /=/ }
      end

      def parameters
        @parameters ||= (
          pms = {}
          collect{ |e| e =~ /=/ }.each do |e|
            pms.store(*split('='))
          end
          pms
        )
      end

      # Does ARGV include a specifc flag?
      def flag?(flag)
        include?(flag)
      end

      # You can use this if you want to use parameterized
      # flags w/o the '=', however be aware that the
      # parameter value will also be listed amoung the
      # bare arguments list. For example:
      #
      #   $ foo tom --say hello
      #
      #   argv.value('--say')  #=> "hello"
      #   argv.arguments  #=> ["tom", "hello"]
      #
      def value(flag)
        fetch(index(flag)+1)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ratch-0.2.1 lib/ratch/argvutils.rb
ratch-0.2.2 lib/ratch/argvutils.rb