# Minimalistic command line option parsing via -s

**URL:** <https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956>\
**Category:** Ruby\
**Created:** [September 6, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956 "2007-09-06T17:36:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 6, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956/1 "2007-09-06T17:36:23Z")

</div>

Hi,

I know, I’m slow, but I finally figured out the minimalistic  
command line options parsing that zenspider et al use in utilities  
like flog and sow (the gem skeleton generator that comes with Hoe).

Consider, for example,

#!/usr/bin/env ruby -s

$r ||= false

if defined? $h then  
puts “usage: #{File.basename $0}”  
puts " -h this usage message"  
puts " -r recursive"  
end

puts “Recursive option: #$r”

The secret sauce is the ‘-s’ option passed to the interpreter,  
which according to the PickAxe indicates, “Any command-line switches  
found after the program filename, but before any filename arguments  
or before a --, are removed from ARGV and set to a global variable  
named for the switch.”

Later,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 6, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956/2 "2007-09-06T17:56:30Z")

</div>

Bil K. wrote:

> $r ||= false  
> which according to the PickAxe indicates, “Any command-line switches  
> found after the program filename, but before any filename arguments  
> or before a --, are removed from ARGV and set to a global variable  
> named for the switch.”

It’s nice to have that for very short scripts, but for about 50 LOC you  
can paste in an options parser that permits --foo style options, -xyz as  
an alias for “-x -y -z”, option arguments, argument conversion procs and  
defaults, “-x123” as an alias for “-x 123”, and even a way to interpret  
“-v -v -v” as thrice verbose. One thing you can’t have is _optional_  
arguments to options (e.g. an option that takes 0 or 1 arguments).

[http://redshift.sourceforge.net/argos.rb](http://redshift.sourceforge.net/argos.rb)

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 6, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956/3 "2007-09-06T23:52:27Z")

</div>

On Sep 6, 2007, at 11:55 AM, Joel VanderWerf wrote:

> It’s nice to have that for very short scripts, but for about 50 LOC  
> you can paste in an options parser that permits --foo style  
> options, -xyz as an alias for “-x -y -z”, option arguments,  
> argument conversion procs and defaults, “-x123” as an alias for “-x  
> 123”, and even a way to interpret “-v -v -v” as thrice verbose. One  
> thing you can’t have is _optional_ arguments to options (e.g. an  
> option that takes 0 or 1 arguments).

A really nice tool I stumbled on while e-stalking Ara Howard (hehe  
sorry) was his main library (gem install main). He’s got some example  
code of it on his site, and it makes options really really easy!

Ari  
-------------------------------------------|  
Nietzsche is my copilot

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 7, 2007, 2:28am UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956/4 "2007-09-07T02:28:38Z")

</div>

Hi,

At Fri, 7 Sep 2007 00:30:05 +0900,  
Bil K. wrote in [ruby-talk:267895]:

> I know, I’m slow, but I finally figured out the minimalistic  
> command line options parsing that zenspider et al use in utilities  
> like flog and sow (the gem skeleton generator that comes with Hoe).

and mkconfig.rb and win32/{mkexports,resource}.rb too.

> Consider, for example,
> 
> #!/usr/bin/env ruby -s

Multiple words in shebang line may not work as you expect.

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 7, 2007, 1:22am UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956/5 "2007-09-07T01:22:34Z")

</div>

Ari B. wrote:

> A really nice tool I stumbled on while e-stalking Ara Howard (hehe  
> sorry) was his main library (gem install main). He’s got some example  
> code of it on his site, and it makes options really really easy!

Just took a look at it… very powerful. It handles arguments (things  
without – or -) as well as options. It’s also fairly complex, and not  
the sort of thing you paste into a small script (almost 2000 lines).

Main looks like more of a complete framework for CLI (possibly like the  
commandline gem–has anyone compared these two?). By contrast, argos  
does just one thing: turn an arg string into a hash, based on a spec.

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 7, 2007, 3:03am UTC](https://www.ruby-forum.com/t/minimalistic-command-line-option-parsing-via-s/113956/6 "2007-09-07T03:03:40Z")

</div>

On Sep 6, 2007, at 10:30 AM, Bil K. wrote:

> $r ||= false  
> which according to the PickAxe indicates, “Any command-line switches  
> found after the program filename, but before any filename arguments  
> or before a --, are removed from ARGV and set to a global variable  
> named for the switch.”

I talk about this some in the summary for this quiz as well:

[http://www.rubyquiz.com/quiz133.html](http://www.rubyquiz.com/quiz133.html)

James Edward G. II
