Ruby_slop_arguments_parser

Slop is an arguments parser for Ruby. It’s installed with gem install slop

I discovered that it can take argument values like this:

  • -v 8
  • -v=8

..but it don’t understand

  • -v:8

I propose a change of parser.rb to this (around line 53):

My version of slop is 4.10.1

# support `foo=bar` and `foo:bar`
orig_flag = flag.dup
if match = flag.match(/([^=:]+)[=:](.*)/)
  flag, arg = match.captures
end

Test Link to heading

require 'slop'

opts = Slop.parse %w|-a 8 -b=8 -c:8 --name:Olle| do |o|
  o.int '-a'
  o.int '-b'
  o.int '-c'
  o.string '--name'
end

#                           before change  |  after change
p [:a, opts[:a]] #          [:a, 8]           [:a, 8]
p [:b, opts[:b]] #          [:b, 8]           [:b, 8]
p [:c, opts[:c]] #          [:c, nil]         [:c, 8]
p [:name, opts[:name]] #    [:name, nil]      [:name, "Olle"]

Another suggestion Link to heading

In types.rb we can see that some types can be abbreviated:

  • integer (can be int)
  • boolean (can be bool)

But string can not be str and symbol can not be sym.

With Slop, you can define your own types.. so every alias will reduce the namespace. Even though.. I suggest:

  • string, str
  • integer, int
  • symbol, sym
  • boolean, bool
  • float, dec
  • array, arr
  • regexp, rex

This can be easily done by modifying types.rb and include:

StrOption = StringOption
SymOption = SymbolOption
DecOption = FloatOption
RexOption = RegexpOption
ArrOption = ArrayOption