summaryrefslogtreecommitdiff
path: root/README.md
blob: a12ae7c1be67cc706c24c4bafbf70852e24ba0b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Slop
====

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

[![Build Status](https://secure.travis-ci.org/injekt/slop.png)](http://travis-ci.org/injekt/slop)

Usage
-----

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

      on 'name=', 'Your name'
      on 'p', 'password', 'An optional 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'
    opts.to_hash   #=> {:name=>"Lee", :password=>nil, :verbose=>true}

Installation
------------

    gem install slop

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*.

Lists
-----

    opts = Slop.parse do
      on :l=, as: Array
    end
    # ruby run.rb -l one,two
    opts[:l] #=> ["one", "two"]
    # ruby run.rb -l one,two -l three
    opts[:l] #=> ["one", "two", "three"]

You can also specify a delimiter and limit.

    opts = Slop.parse do
      on :l=, as: Array, delimiter: ':', limit: 2
    end
    # ruby run.rb -l one:two:three
    opts[:l] #=> ["one", "two:three"]

Ranges
------

    opts = Slop.parse do
      on :r=, as: Range
    end
    # ruby run.rb -r 1..10
    opts[:r] #=> 1..10
    # ruby run.rb -r 1...10
    opts[:r] #=> 1...10
    # ruby run.rb -r 1-10
    opts[:r] #=> 1..10
    # ruby run.rb -r 1,10
    opts[:r] #=> 1..10

Autocreate
----------

Slop has an 'autocreate' feature. This feature is intended to create
options on the fly, without having to specify them yourself. In some case,
uses this code could be all you need in your application:

    # ruby run.rb --foo bar --baz --name lee
    opts = Slop.parse(autocreate: true)
    opts.to_hash #=> {:foo=>"bar", :baz=>true, :name=>"lee"}
    opts.fetch_option(:name).expects_argument? #=> true

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 }