Project

dreck

0.0
No commit activity in last 3 years
No release in over 3 years
Typechecks and coerces non-option arguments.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

dreck

license Build Status Gem Version

A stupid parser for trailing arguments.

Motivation

There are lots of good argument parsers for Ruby: OptionParser, Slop, trollop, cocaine, etc.

Most of these are great for parsing and typechecking options, but none provide a good interface to the arguments that often trail the options.

Dreck does exactly that. Give it a specification of arguments (and their types) to expect, and it will give you a nicely typechecked result.

Installation

$ gem install dreck

Usage

Dreck is best used in conjunction with an option parser like Slop, which can provide an array of arguments with options already filtered out:

opts = Slop.parse do |s|
  # ...
end

opts.args # => [ "/dev/urandom", "/dev/sda", "512" ]

results = Dreck.parse opts.args, strict: true do
  file   :input
  symbol :output
  int    :blocksize
end

results[:input] # => "/dev/urandom"
results[:output] # => :"/dev/sda"
results[:blocksize] # => 512

Lists are also supported:

result = Dreck.parse opts.args do
  string :uuid
  list   :file, :inputs, count: 2
  list   :int, :nums
end

result[:uuid] # => "01aa84ab-5b2c-4861-adc9-fcc6990a5ca5"
result[:inputs] # => ["/tmp/foo", "/tmp/bar"]
result[:nums] # => [1, 2, 3, 4, 5]

TODO

  • Guarding against multiple unbound lists/unbound list before scalar types
  • Custom types?