[ANN] Slop 1.3.0

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’, :optional => false # the same thing
on :a, :age, ‘Your age’, :optional => 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 J.’, ‘verbose’ => true, ‘age’ =>
nil, ‘sex’ => ‘male’}

Symbols

opts.to_hash(true) #=> {:name => ‘Lee J.’, :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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs