[ANN] Slop 1.3.0

L

Lee Jarvis

Slop
====

Website: http://github.com/injekt/slop

Slop is a simple option collector with an easy to remember syntax and
friendly API.

Installation
--------

### Rubygems

gem install slop

### GitHub

git clone git://github.com/injekt/slop.git
gem build slop.gemspec
gem install slop-<version>.gem

Usage
-----
# parse assumes ARGV, otherwise you can pass it your own Array
opts = Slop.parse do
on :v, :verbose, 'Enable verbose mode' # boolean value
on :n, :name, 'Your name', true # compulsory argument
on :s, :sex, 'Your sex', :eek:ptional => false # the same thing
on :a, :age, 'Your age', :eek:ptional => true # optional argument
end

# if ARGV is `-v --name 'lee jarvis' -s male`
opts.verbose? #=> true
opts.name? #=> true
opts[:name] #=> 'lee jarvis'
opts.age? #=> false
opts[:age] #=> nil

You can also return your options as a Hash

opts.to_hash #=> {'name' => 'Lee Jarvis', 'verbose' => true, 'age' =>
nil, 'sex' => 'male'}

# Symbols
opts.to_hash(true) #=> {:name => 'Lee Jarvis', :verbose => true, :age
=> nil, :sex => 'male'}

If you don't like the method `on` (because it sounds like the option
**expects**
a block), you can use the `opt` or `option` alternatives.

on :v, :verbose
opt :v, :verbose
option :v, :verbose

If you don't like that Slop evaluates your block, or you want slop
access
inside of your block without referring to `self`, you can pass a block
argument to
`parse`.

Slop.parse do |opts|
opts.on :v, :verbose
opts.on :n, :name, 'Your name', true
end

If you want some pretty output for the user to see your options, you can
just
send the Slop object to `puts` or use the `help` method.

puts opts
puts opts.help

Will output something like

-v, --verbose Enable verbose mode
-n, --name Your name
-a, --age Your age



This version adds support for parsing multiple syntaxes from the command
line, see https://gist.github.com/890309 for an example
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[ANN] Slop 1.5.0 0
[ANN] getopt-1.3.0 5
[ANN] CruiseControl.rb 1.3.0 0
[ANN] nokogiri 1.3.0 Released 14
[ANN] directory_watcher 1.3.0 0
[ANN] mail Gem version 1.3.0 0
[ANN] JRuby 1.3.0 Released 0
[ANN] map-1.5.0 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top