Note that the block we've added to the --version flag will be executed
during parse time. Therefore these blocks should be reserved
for immediately reacting to the presence of a flag. If you want to
access other options or mutate values, check out the "Custom option types"
section below and implement the #finish method.

If you want to disable the built-in string-splitting, set the delimiter to
nil.

Custom option types

Slop uses option type classes for every new option added. They default to the
NullOption. When you type o.array Slop looks for an option called
Slop::ArrayOption. This class must contain at least 1 method, call. This
method is executed at parse time, and the return value of this method is
used for the option value. We can use this to build custom option types:

Custom options can also implement a finish method. This method by default
does nothing, but it's executed once all options have been parsed. This
allows us to go back and mutate state without having to rely on options
being parsed in a particular order. Here's an example:

moduleSlopclassFilesOption<ArrayOptiondeffinish(opts)ifopts.expand?self.value=value.map{|f|File.expand_path(f)}endendendendopts=Slop.parse%w(--filesfoo.txt,bar.rb-e)do|o|o.files'--files','an array of files'o.bool'-e','--expand','if used, list of files will be expanded'endpopts[:files]#=> ["/full/path/foo.txt", "/full/path/bar.rb"]

Errors

Slop will raise errors for the following:

An option used without an argument when it expects one: Slop::MissingArgument

An option used that Slop doesn't know about: Slop::UnknownOption

An option marked as required when not provided: Slop::MissingRequiredOption

These errors inherit from Slop::Error, so you can rescue them all.
Alternatively you can suppress these errors with the suppress_errors config
option:

opts=Slop.parsesuppress_errors:truedoo.string'-name'end# or per option:
opts=Slop.parsedoo.string'-host',suppress_errors:trueo.int'-port'end

Printing help

The return value of Slop.parse is a Slop::Result which provides a nice
help string to display your options. Just puts opts or call opts.to_s:

It'll deal with aligning your descriptions according to the longest option
flag.

Here's an example of adding your own help option:

o.on'--help'doputsoexitend

Commands

As of version 4, Slop does not have built in support for git-style subcommands.
You can use version 3 of Slop (see v3 branch). I also expect there to be some
external libraries released soon that wrap around Slop to provide support for
this feature. I'll update this document when that happens.

Upgrading from version 3

Slop v4 is completely non-backwards compatible. The code has been rewritten
from the ground up. If you're already using version 3 you have to update
your code to use version 4. Here's an overview of the more fundamental changes:

No more as for option types

No more trailing =

Instead, the "does this option expect an argument" question is answered by
the option type (i.e on and bool options do not expect arguments, all
others do. They handle type conversion, too.

Hyphens are required

This was a hard decision to make, but you must provide prefixed hyphens when
declaring your flags. This makes the underlying code much nicer and much less
ambiguous, which leads to less error prone code. It also means you can easily
support single hyphen prefix for a long flag, i.e -hostname which you
could not do before. It also provides a hidden feature, which is infinity flag
aliases: o.string '-f', '-x', '--foo', '--bar', 'this is insane'

Strict is now on by default

v3 had a strict option. v4 has no such option, and to suppress errors you can
instead provide the suppress_errors: true option to Slop.

No more parse!

Where v3 has both Slop.parse and Slop.parse!, v4 only has parse. The
former was used to decide whether Slop should or should not mutate the
original args (usually ARGV). This is almost never what you want, and it
can lead to confusion. Instead, Slop::Result provides an arguments
methods: