Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Ruby

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
example
lib
test
.gitignore
.travis.yml
.yardopts
Gemfile
MIT-LICENSE
README.md
Rakefile
optionalargument.gemspec

README.md

optionalargument

Build Status Gem Version

Description

Revenge of the Hash options. Hash will beat keyword arguments!!

Features

  • Flexible and readable definitions
  • Strict parsing for key combinations
  • Key compatible for Symbol<->String
  • Validate and coerce values
  • You can use parsed options as Struct
  • Pure Ruby :)

Usage

Of course, you can mix following features :)
Clean up DEFUALT_OPTIONS.merge(options) and validations!

Basic API

require 'optionalargument'

class Foo
  FuncOptArg = OptionalArgument.define {
    opt :a, must: true
    opt :b
  }

  def func(options={})
    opts = FuncOptArg.parse options
    p opts.a
    p opts.b?
    p opts.b
  end
end

foo = Foo.new
foo.func a: 1           #=> opts.a => 1, opts.b? => false, opts.b => nil
foo.func a: 1, "b" => 2 #=> opts.a => 1, opts.b? => true, opts.b => 2
foo.func "b" => 2       #=> Error (`a` is must, but not passed)
foo.func a:1, c: 3      #=> Error (`c` is not defined)

Key combinations

OptArg = OptionalArgument.define {
  opt :a
  opt :b
  conflict :a, :b
  opt :c, requirements: [:b, :d]
  opt :d, aliases: [:d2, :d3]
  opt :e, deprecateds: [:e2, :e3]
}

OptArg.parse(a: 1, b: 1) #=> Error: conflict conbination thrown: a, b'
OptArg.parse(c: 1)       #=> Error: `c` requires  `b` and `d`
OptArg.parse(d2: 1).d3   #=> 1
OptArg.parse(e2: 1).e3   #=> 1 with warning "`e2` is deprecated, use `e`" 

Validate and coerce value

OptArg = OptionalArgument.define {
  opt :x, condition: 3..5
  opt :y, condition: AND(Float, 3..5)
  opt :z, adjuster: ->arg{Float arg}
}

OptArg.parse x: 5       #=> pass : 5 is sufficient for 3..5
OptArg.parse x: 6       #=> Error: 6 is deficient for 3..5
OptArg.parse y: 5       #=> Error: 5 is deficient for Float
OptArg.parse y: 5.0     #=> pass : 5.0 is sufficient for 3..5 and Float
OptArg.parse(z: '1').z  #=> 1.0  : casted under adjuster

Default value

OptArg = OptionalArgument.define {
  opt :a
  opt :b, default: 'This is a default value'
}

OptArg.parse(a: 1).b  #=> 'This is a default value'

Relax parsing

Builtin features are designed by relax parsing for unknown options.

OptArg = OptionalArgument.define {
  opt :known, must: true
}

opts = OptArg.parse(
         {known: 1, unknown: 2},
         defined_only: false)    #=> pass

Switch error

OptArg = OptionalArgument.define {
  opt :a
}

OptArg.parse({b: 1}, exception: ArgumentError) #=> ArgumentError
OptArg.parse({b: 1}, exception: KeyError)      #=> KeyError

Requirements

Install

gem install optionalargument

Link

License

The MIT X11 License
Copyright (c) 2012 Kenichi Kamiya
See MIT-LICENSE for further details.

Something went wrong with that request. Please try again.