title: Really Cheap Command-Line Option Parsing in Ruby
body: <p>EDIT: Please, <em>please</em>, <strong>please</strong> read the two requirements listed at the bottom of this post before replying. People keep posting their new gems and libraries and whatnot, which clearly don't meet the requirements.</p>
  
  <p>Sometimes I want to very cheaply hack some command line options into a simple script. A fun way to do it, without dealing with getopts or parsing or anything like that, is:</p>
  
  <pre><code>...
  $quiet       = ARGV.delete('-d')
  $interactive = ARGV.delete('-i')
  ...
  # Deal with ARGV as usual here, maybe using ARGF or whatever.
  </code></pre>
  
  <p>It's not quite the normal Unix options syntax, because it will accept options non-option command line parameters, as in "<code>myprog -i foo bar -q</code>", but I can live with that. (Some people, such as the Subversion developers, prefer this. Sometimes I do too.)</p>
  
  <p>An option that's just present or absent can't be implemented much more simply than the above. (One assignment, one function call, one side effect.) Is there an equally simple way to deal with options that take a parameter, such as "<code>-f</code> <em>filename</em>"?</p>
  
  <h3>EDIT:</h3>
  
  <p>One point I didn't make earlier on, because it hadn't become clear to me until the author of Trollop mentioned that the library fit "in one [800-line] file," is that I'm looking not only for clean syntax, but for a technique that has the following characteristics:</p>
  
  <ol>
  <li><p>The entirety of the code can be included in the script file (without overwhelming the actual script itself, which may be only a couple of dozen lines), so that one can drop a single file in a <code>bin</code> dir on any system with a standard Ruby 1.8.[5-7] installation and use it. If you can't write a Ruby script that has no require statements and where the code to parse a couple of options is under a dozen lines or so, you fail this requirement.</p></li>
  <li><p>The code is small and simple enough that one can remember enough of it to directly type in code that will do the trick, rather than cutting and pasting from somewhere else. Think of the situation where you're on the console of a firewalled sever with no Internet access, and you want to toss together a quick script for a client to use. I don't know about you, but (besides failing the requirement above) memorizing even the 45 lines of simplified micro-optparse is not something I care to do.</p></li>
  </ol>