https://github.com/veelenga/raph
Ruby Argument Parsing for Humans
https://github.com/veelenga/raph
Last synced: 2 months ago
JSON representation
Ruby Argument Parsing for Humans
- Host: GitHub
- URL: https://github.com/veelenga/raph
- Owner: veelenga
- License: other
- Created: 2015-01-28T07:21:10.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-08-27T11:08:15.000Z (almost 10 years ago)
- Last Synced: 2024-05-21T11:30:30.984Z (about 1 year ago)
- Language: Ruby
- Size: 393 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# raph [](https://rubygems.org/gems/raph) [](https://travis-ci.org/veelenga/raph)

**R**uby **A**rgument **P**arsing for **H**umans
Inspired by [args](https://github.com/kennethreitz/args)
## Installation:
`$ gem install raph`
## Usage:
Here is application sample:
```ruby
# 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:
```sh
$ ruby sample.rb
Arguments passed in: []
Flags detected: []
Files detected: []
Assignments detected: {}
Grouped arguments: {}
```If you have few arguments passed:
```sh
$ 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:
```sh
$ 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:
```ruby
require 'raph'include Raph
class AnimalParser < BaseParser
ANIMALS = ['cat', 'dog', 'pig', 'bear', 'elephant']def id
:animals
enddef parse(args)
animals = []
args.each do |arg|
animals << arg if ANIMALS.include? arg.strip.downcase
end
animals
end
endargs = [ '--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"]```