Git Product home page Git Product logo

slop's Introduction

Slop

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

This README is targeted at Slop v3.

Build Status

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
  banner "ruby foo.rb [options]\n"
  on :name=, 'Your name'
  on :p, :password, 'Your password', :argument => :optional
  on :v, :verbose, 'Enable verbose mode'
end

# if ARGV is `--name Lee -v`
opts.verbose?  #=> true
opts.password? #=> false
opts[:name]    #=> 'lee'

Slop supports several methods of writing options:

# These options all do the same thing
on '-n', '--name', 'Your name', :argument => true
on 'n', :name=, 'Your name'
on :n, '--name=', 'Your name'

# As do these
on 'p', '--password', 'Your password', :argument => :optional
on :p, :password, 'Your password', :optional_argument => true
on '-p', 'password=?', 'Your password'

You can also return your options as a Hash:

opts.to_hash #=> { :name => 'lee', :verbose => true, :password => nil }

Printing Help

Slop attempts to build a good looking help string to print to your users. You can see this by calling opts.help or simply puts opts.

Configuration Options

All of these options can be sent to Slop.new or Slop.parse in Hash form.

  • strict - Enable strict mode. When processing unknown options, Slop will raise an InvalidOptionError. default: false.
  • help - Automatically add the --help option. default: false.
  • banner - Set this options banner text. default: nil.
  • ignore_case - When enabled, -A will look for the -a option if -A does not exist. default: false.
  • autocreate - Autocreate options on the fly. default: false.
  • arguments - Force all options to expect arguments. default: false.
  • optional_arguments - Force all options to accept optional arguments. default: false.
  • multiple_switches - When disabled, Slop will parse -abc as the option a with the argument bc rather than 3 separate options. default: true.
  • longest_flag - The longest string flag, used to aid configuring help text. default: 0.

Features

Check out the following wiki pages for more features:

Woah woah, why you hating on OptionParser?

I'm not, honestly! I love OptionParser. I really do, it's a fantastic library. So why did I build Slop? Well, I find myself using OptionParser to simply gather a bunch of key/value options, usually you would do something like this:

require 'optparse'

things = {}

opt = OptionParser.new do |opt|
  opt.on('-n', '--name NAME', 'Your name') do |name|
    things[:name] = name
  end

  opt.on('-a', '--age AGE', 'Your age') do |age|
    things[:age] = age.to_i
  end

  # you get the point
end

opt.parse
things #=> { :name => 'lee', :age => 105 }

Which is all great and stuff, but it can lead to some repetition. The same thing in Slop:

require 'slop'

opts = Slop.parse do
  on :n, :name=, 'Your name'
  on :a, :age=, 'Your age', :as => :int
end

opts.to_hash #=> { :name => 'lee', :age => 105 }

slop's People

Contributors

basicxman avatar dominikh avatar amon-sha avatar unpublishedworks avatar no6v avatar denisdefreyne avatar eric1234 avatar jimmycuadra avatar lolmaus avatar conradirwin avatar utkarshkukreti avatar chsonnu avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.