Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

165 lines (115 sloc) 3.777 kb


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

NOTE This branch refers to the prerelease 1.0.0.rc1 version, this version has incompatible changes from version 0.0.3 (the latest stable version). You should upgrade.



gem install slop --pre


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


# 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 :a, :age, 'Your age', :optional => true   # optional argument

# if ARGV is `-v --name 'lee jarvis'`
opts.verbose? #=> true    #=> 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}

If you don't like the method on (because it sounds like the option expects a callback), 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

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

Will output something like

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

You can also add a banner using the banner method

opts = Slop.parse
opts.banner = "Usage: foo.rb [options]"


opts = Slop.parse do
  banner "Usage: foo.rb [options]"


If you'd like to trigger an event when an option is used, you can pass a block to your option. Here's how:

Slop.parse do
  on :V, :version, 'Print the version' do
    puts 'Version 1.0.0'

Now when using the --version option on the command line, the trigger will be called and its contents executed.

Ugh, Symbols

Fine, don't use them

Slop.parse do
  on :n, :name, 'Your name'
  on 'n', 'name', 'Your name'
  on '-n', '--name', 'Your name'

All of these options will do the same thing

Ugh, Blocks

C'mon man, this is Ruby, GTFO if you don't like blocks.

opts =
opts.on :v, :verbose


Slop is pretty smart when it comes to building your options, for example if you want your option to have a flag attribute, but no --option attribute, you can do this:

on :n, "Your name"

and Slop will detect a description in place of an option, so you don't have to do this:

on :n, nil, "Your name", true

You can also try other variations:

on :name, "Your name"
on :n, :name
on :name, true


You can of course also parse lists into options. Here's how:

opts = Slop.parse do
  opt :people, true, :as => Array

# ARGV is `--people lee,john,bill`
opts[:people] #=> ['lee', 'john', 'bill']

You can also change both the split delimiter and limit

opts = Slop.parse do
  opt :people, true, :as => Array, :delimiter => ':', :limit => 2)
opts[:people] #=> ["lee", "injekt:bob"]


If you'd like to contribute to Slop (it's really appreciated) please fork the GitHub repository, create your feature/bugfix branch, add tests, and send me a pull request. I'd be more than happy to look at it.

Jump to Line
Something went wrong with that request. Please try again.