README.md in slop-1.2.0 vs README.md in slop-1.2.1
- old
+ new
@@ -11,12 +11,12 @@
gem install slop
### GitHub
git clone git://github.com/injekt/slop.git
- gem build slop.gemspec
- gem install slop-<version>.gem
+ 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
@@ -38,27 +38,10 @@
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 pass a block to `Slop#parse`, Slop will yield non-options as
-they're found, just like
-[OptionParser](http://rubydoc.info/stdlib/optparse/1.9.2/OptionParser:order)
-does it.
-
- opts = Slop.new do
- on :n, :name, :optional => false
- end
-
- opts.parse do |arg|
- puts arg
- end
-
- # if ARGV is `foo --name Lee bar`
- foo
- bar
-
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
@@ -79,11 +62,11 @@
puts opts
puts opts.help
Will output something like
- -v, --verbose Enable verbose mode
+ -v, --verbose Enable verbose mode
-n, --name Your name
-a, --age Your age
You can also add a banner using the `banner` method
@@ -94,26 +77,82 @@
opts = Slop.parse do
banner "Usage: foo.rb [options]"
end
+Helpful Help
+------------
+
+Long form:
+
+ Slop.parse do
+ ...
+ on :h, :help, 'Print this help message', :tail => true do
+ puts help
+ exit
+ end
+ end
+
+Shortcut:
+
+ Slop.parse :help => true do
+ ...
+ end
+
Callbacks
---------
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'
- exit
- end
- end
+ Slop.parse do
+ on :V, :version, 'Print the version' do
+ puts 'Version 1.0.0'
+ exit
+ end
+ end
Now when using the `--version` option on the command line, the trigger will
be called and its contents executed.
+Yielding Non Options
+--------------------
+
+If you pass a block to `Slop#parse`, Slop will yield non-options as
+they're found, just like
+[OptionParser](http://rubydoc.info/stdlib/optparse/1.9.2/OptionParser:order)
+does it.
+
+ opts = Slop.new do
+ on :n, :name, :optional => false
+ end
+
+ opts.parse do |arg|
+ puts arg
+ end
+
+ # if ARGV is `foo --name Lee bar`
+ foo
+ bar
+
+Negative Options
+----------------
+
+Slop also allows you to prefix `--no-` to an option which will force the option
+to return a false value.
+
+ opts = Slop.parse do
+ on :v, :verbose, :default => true
+ end
+
+ # with no command line options
+ opts[:verbose] #=> true
+
+ # with `--no-verbose`
+ opts[:verbose] #=> false
+ opts.verbose? #=> false
+
Ugh, Symbols
------------
Fine, don't use them
@@ -139,22 +178,22 @@
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"
+ 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
+ on :n, nil, "Your name", true
You can also try other variations:
- on :name, "Your name"
- on :n, :name
- on :name, true
+ on :name, "Your name"
+ on :n, :name
+ on :name, true
Lists
-----
You can of course also parse lists into options. Here's how:
@@ -167,16 +206,55 @@
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)
+ opt :people, true, :as => Array, :delimiter => ':', :limit => 2)
end
# ARGV is `--people lee:injekt:bob`
opts[:people] #=> ["lee", "injekt:bob"]
+Strict Mode
+-----------
+
+Passing `strict => true` to `Slop.parse` causes it to raise a `Slop::InvalidOptionError`
+when an invalid option is found (`false` by default):
+
+ Slop.new(:strict => true).parse(%w/--foo/)
+ # => Slop::InvalidOptionError: Unknown option -- 'foo'
+
+and it handles multiple invalid options with a sprinkling of pluralization:
+
+ Slop.new(:strict => true).parse(%w/--foo --bar -z/)
+ # => Slop::InvalidOptionError: Unknown options -- 'foo', 'bar', 'z'
+
+Significantly, however, Slop will still parse the valid options:
+
+ begin
+ slop = Slop.new(:strict => true, :help => true) do
+ banner "Usage:\n\t./awesome_sauce [options]\n\nOptions:"
+ on :n, :name, 'Your name'
+ end
+ slop.parse(%w/--foo --bar -z/)
+ rescue Slop::InvalidOptionError => e
+ puts "\n#{e.message}\n\n"
+ puts slop
+ exit
+ end
+
+yields:
+
+ Unknown options -- 'foo', 'bar', 'z'
+
+ Usage:
+ ./awesome_sauce [options]
+
+ Options:
+ -n, --name Your name
+ -h, --help Print this help message
+
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
@@ -209,13 +287,6 @@
opts = Slop.parse do
on :n, :name, 'Your name', true
on :a, :age, 'Your age', true
end
- things = opts.to_hash
-
-Contributing
-------------
-
-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.
+ things = opts.to_hash
\ No newline at end of file