Ropet

Ropet is a helper library for easy integration of options in methods.

Concept

An option can be a flag, for example:

  :debug
  :no_debug

or a standard hash option:

  :debug
  {threads: 3}
  # or, if you prefer:
  {:toolkit => :gcc}

Quick usage

In a method accepting *options as the last argument, you can use:

  # remember to pass `*options` (with an asterisk), because it
  # passes `options.length` instead of an array in one argument
  # and #parser doesn't expect one array
  Ropet.parser *options

  # or manual options
  parser = Ropet.parser :debug, threads: 2

  parser[:debug]  #=> true
  parser[:threads]#=> 2

  parser.inject *options
  parser.inject :no_debug, toolkit: :gcc

Detailed usage

In addition to standard *options in Ropet#parser, you can also pass a block with configuration, which executes before injecting/interpreting options.

Ropet::Config provides a small DSL for basic configuration, like this:

  parser = Ropet.parser :without_debug do
    prefix 'without_'                 # flags starting with 'without_' will be interpreted
                                      # as false booleans
    default :library_path, './lib'    # when `library_path` is not set,
                                      # it will be taken from defaults list
  end

  parser[:debug]                      #=> false
  parser[:library_path]               #=> "./lib"

  parser.inject library_path: "../library"
                                      # `#inject` accepts the same kind of
                                      # *options that `Ropet#parser`

  parser[:library_path]               #=> "../library"

  parser.inject :without_without_templates
                                      # for extreme cases, when key names include prefixes
                                      # it is advisable to change false_prefix with dsl method
                                      # but you probably won't have a key names no_no_*

  parser.has_key? :without_templates  #=> true

  parser.settings                     #=> {:debug=>false, :library_path=>"../library", :without_templates=>false}

  # you can also access defaults whenever you want,
  # they're not treated in the same way as other options
  # (which are overwritten in case of conflicts)
  parser.config.defaults            #=> {:library_path=>"./lib"}

Copyright

Copyright © 2012 Jakub Okoński

Creative Commons License
Ropet by Jakub Okoński is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.