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}"
If you do not pass any arguments:
$ ruby sample.rb
Arguments passed in: []
Flags detected: []
Files detected: []
Assignments detected: []
If you have few arguments passed:
$ ruby sample.rb -v --flag1 --flag2 --formatter=simple --convert=true
Arguments passed in: ["-v", "--flag1", "--flag2", "--formatter=simple", "--convert=true"]
Flags detected: [:v, :flag1, :flag2]
Files detected: []
Assignments detected: {:formatter=>"simple", :convert=>"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: {}
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"]
TODO:
- Grouped arguments parser.
- Not-files parser.