# Build a hash from ARGV, the Ruby way

**URL:** <https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281>\
**Category:** Ruby\
**Created:** [January 26, 2010, 12:00am UTC](https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281 "2010-01-26T00:00:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [January 26, 2010, 12:00am UTC](https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281/1 "2010-01-26T00:00:23Z")

</div>

I would like to build a hash from ARGV using UNIX type notation ie any  
argument which begins with a hyphen or double hyphen is an option and  
may have as value(s) the argument(s) which succeed it (if they don’t  
have hyphens). Options without values should result in a hash element  
which has a key and a dummy value (say “0”).

I suspect/hope there is a very simple “Ruby way” to do this, but I got  
stuck thinking how to look ahead when iterating through ARGV. I’ve got  
a bodged method of just taking pairs eg -a 1 -b 0 -c 3 etc, but it would  
be nice to it properly. Any suggestions? All help appreciated!

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [January 26, 2010, 12:41am UTC](https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281/2 "2010-01-26T00:41:02Z")

</div>

On Jan 25, 6:00 pm, Toby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like to build a hash from ARGV using UNIX type notation ie any  
> argument which begins with a hyphen or double hyphen is an option and  
> may have as value(s) the argument(s) which succeed it (if they don’t  
> have hyphens). Options without values should result in a hash element  
> which has a key and a dummy value (say “0”).
> 
> I suspect/hope there is a very simple “Ruby way” to do this, but I got  
> stuck thinking how to look ahead when iterating through ARGV. I’ve got  
> a bodged method of just taking pairs eg -a 1 -b 0 -c 3 etc, but it would  
> be nice to it properly. Any suggestions? All help appreciated!

$ gem install facets  
$ irb

> require ‘facets/argvector’  
> argv = Argvector.new(“-a 0 -b 0 -c 3”)  
> argv.options

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [January 26, 2010, 11:25am UTC](https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281/3 "2010-01-26T11:25:44Z")

</div>

Toby R. wrote:

> I would like to build a hash from ARGV using UNIX type notation ie any  
> argument which begins with a hyphen or double hyphen is an option and  
> may have as value(s) the argument(s) which succeed it (if they don’t  
> have hyphens). Options without values should result in a hash element  
> which has a key and a dummy value (say “0”).

Take a look at optparse.rb in the standard ruby library:  
[http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html)

Then if you don’t like that, there are several other libraries that  
other people have written with more “object like” APIs and very  
forgettable names. One I came across recently was thor:

> **[GitHub - rails/thor: Thor is a toolkit for building powerful command-line...](https://github.com/rails/thor)**
>
> Thor is a toolkit for building powerful command-line interfaces. - GitHub - rails/thor: Thor is a toolkit for building powerful command-line interfaces.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [January 26, 2010, 11:37am UTC](https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281/4 "2010-01-26T11:37:38Z")

</div>

Brian C. wrote:

> Then if you don’t like that, there are several other libraries that  
> other people have written with more “object like” APIs and very  
> forgettable names.

[http://www.rubyinside.com/trollop-command-line-option-parser-for-ruby-944.html](http://www.rubyinside.com/trollop-command-line-option-parser-for-ruby-944.html)

(and some other alternatives are given in the body and the comments)

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [January 26, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/build-a-hash-from-argv-the-ruby-way/182281/5 "2010-01-26T19:09:06Z")

</div>

Reformatted excerpts from Toby R.'s message of 2010-01-25:

> I suspect/hope there is a very simple “Ruby way” to do this, but I got  
> stuck thinking how to look ahead when iterating through ARGV. I’ve  
> got a bodged method of just taking pairs eg -a 1 -b 0 -c 3 etc, but it  
> would be nice to it properly. Any suggestions? All help appreciated!

You can start doing something simple with ARGV.each\_slice(2), but any  
kind of error checking quickly gets complicated. Trollop will do this  
exact thing for you (turn ARGV into a hash with error checking).
