raph Gem Version Build Status

This is Raph

Ruby Argument Parsing for Humans

Inspired by args

Installation:

$ gem install raph

Usage:

Here is application sample:

# sample.rb
require 'raph'

puts "Arguments passed in:  #{$raph.all}"
puts "Flags detected:       #{$raph.flags}"
puts "Files detected:       #{$raph.files}"
puts "Assignments detected: #{$raph.assignments}"
puts "Grouped arguments:    #{$raph.grouped_args}"

If you do not pass any arguments:

$ ruby sample.rb
Arguments passed in:  []
Flags detected:       []
Files detected:       []
Assignments detected: {}
Grouped arguments:    {}

If you have few arguments passed:

$ ruby sample.rb -v 1 2 3 --flag1 3 --flag2 --formatter=simple true
Arguments passed in:  ["-v", "1", "2", "3", "--flag1", "3", "--flag2", "--formatter=simple", "true"]
Flags detected:       [:v, :flag1, :flag2]
Files detected:       []
Assignments detected: {:formatter=>"simple"}
Grouped arguments:    {:v=>["1", "2", "3"], :flag1=>["3"], :flag2=>[], :"formatter=simple"=>["true"]}

And finnaly if you pass expanded arguments:

$ ruby sample.rb -f spec/*.rb
Arguments passed in:  ["-f", "spec/raph_spec.rb", "spec/spec_helper.rb"]
Flags detected:       [:f]
Files detected:       ["spec/raph_spec.rb", "spec/spec_helper.rb"]
Assignments detected: {}
Grouped arguments:    {:f=>["spec/raph_spec.rb", "spec/spec_helper.rb"]}

Advanced usage:

You can use raph with custom parsers. For example:

require 'raph'

include Raph

class AnimalParser < BaseParser
  ANIMALS = ['cat', 'dog', 'pig', 'bear', 'elephant']

  def id
    :animals
  end

  def parse(args)
    animals = []
    args.each do |arg|
      animals << arg if ANIMALS.include? arg.strip.downcase
    end
    animals
  end
end

args = [ '--my-animals', 'cat', 'bird', 'dog', 'elephant' ]

raph = Raph::Raph.new.tap do |r|
  r.add_parser( AnimalParser.new )
  r.parse( args )
end

# Raph#animals attribute is added dynamically.
# It is defined by AnimalParser#id method.
puts "All:        #{raph.all}"
puts "My animals: #{raph.animals}"

#All:        ["--my-animals", "cat", "bird", "dog", "elephant"]
#My animals: ["cat", "dog", "elephant"]