Command-line option parsing

Discussion in 'Ruby' started by Eric J. Roode, Dec 27, 2005.

  1. Greetings,

    I'm new to ruby (from Perl). I notice there are several command-line
    options parsing modules. The two that stuck out were GetoptLong and
    OptionsParser. GetoptLong suffers from being "the same old Getopt".
    OptionsParser claims to be "more advanced and more ruby-like", but has
    extremely poor documentation.

    Are these the best two modules out there? Is there an emerging
    "standard" module? (In Perl, Getopt::Long is by far the most commonly-used

    (I apologize if this is a FAQ; I read the FAQ and did a google scan of past
    articles. Most of what I found was authors announcing their own options-
    parsing modules).

    Thanks in advance,
    Eric J. Roode, Dec 27, 2005
    1. Advertisements

  2. Eric J. Roode

    Lou Vanek Guest

    Lou Vanek, Dec 28, 2005
    1. Advertisements

  3. I used to use GetopLong in the beginning but switched to OptionParser.
    Agreee, the documentation could be better but if you look carefully at the
    example given in RDoc you get pretty much everything you need. I'm not sure
    about a standard parser but my impression was that OptionParser is more
    widely used. But I may be wrong here.

    Kind regards

    Robert Klemme, Dec 28, 2005
  4. Eric J. Roode

    Jim Freeze Guest

    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    That is old documenetation and the link on Rubyforge was to be replaced eon=
    ago. The new docs are at:

    There you will find the option parser docs after Application section. For
    the most part, if you use the CommandLine::Application class, you don't nee=
    to get
    your hands that dirty with CommandLine::OptionParser.

    If you have any questions, just post a question or send me
    a private email. I plan to post a video on the usage this week. Maybe a
    rails like video will generate more interest. :)
    Jim Freeze, Dec 28, 2005
  5. Eric J. Roode

    Steve Litt Guest

    Does OptionParser come with standard Ruby?


    Steve Litt
    Steve Litt, Dec 28, 2005
  6. Will there be stunt scenes with options and a high speed car chase?

    Robert Klemme, Dec 28, 2005
  7. Eric J. Roode

    Jim Freeze Guest

    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    It will be action packed. There will be trains, rubies
    and high speed typing! ;)
    Jim Freeze, Dec 28, 2005
  8. Well... that's not *bad*, but I guess I had expected something a bit more
    ruby-like. Whatever that means. :)

    Eric J. Roode, Dec 28, 2005
  9. I disagree about the example being all you need.

    Here's the first option in the example:

    # Mandatory argument.
    opts.on("-r", "--require LIBRARY",
    "Require the LIBRARY before executing your script") do |lib|
    options.library << lib

    I am new to ruby, so I don't know what "options.library << lib" means. I
    thought that that introduced a here-doc string.

    The second example:

    # Optional argument; multi-line description.
    opts.on("-i", "--inplace [EXTENSION]",
    "Edit ARGV files in place",
    " (make backup if EXTENSION supplied)") do |ext|

    It took me a lot of staring at the code before it dawned on me that it
    was the *brackets* that made it an optional argument.

    The final example:

    # Another typical switch to print the version.
    opts.on_tail("--version", "Show version") do
    puts OptionParser::Version.join('.')

    I can't figure out how the on_tail method differs from the on method or
    the on_head method. Reading the source isn't helping. The example

    The previous example has this comment:

    # No argument, shows at tail. This will print an options summary.

    "Shows at tail"??

    At the end of the parse() method (and why is it self.parse, not just
    parse?), there is this:


    I can't tell what function that performs for the class.

    I can't tell how to make an option mandatory, how to configure it so that
    some options require certain others or conflict with certain others.

    It looks like a fine, powerful module. But its lack of documentation
    makes it pretty much useless for anyone who doesn't already know how to
    use it.

    I come from the Perl world. You simply do not release a module to Perl's
    CPAN unless it is thoroughly documented. The vast majority of CPAN
    modules have quite good documentation -- or at least, thorough
    documentation. I'm quite surprised to find that the situation is so
    different in the ruby world.

    Eric J. Roode, Dec 28, 2005
  10. Eric J. Roode

    Jim Freeze Guest

    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Hi Eric

    You may want to give CommandLine a try.
    From the examples you give, this is how I would write an app:

    require 'rubygems'
    require 'commandline'

    class MyApp < CommandLine::Application
    def initialize
    # Mandatory argument
    option :names =3D> %w(--require -r),
    :eek:pt_description =3D> "Require the LIBRARY "+
    "before executing your script",
    :arg_description =3D> "LIBRARY",
    :eek:pt_found =3D> get_arg,
    :eek:pt_not_found =3D> required

    option :names =3D> %w(--inplace -i),
    :arity =3D> [0,1],
    :eek:pt_description =3D> "Edit ARGV files in place",
    :arg_description =3D> "[EXTENSION]",
    :eek:pt_found =3D> get_arg,

    def main
    #put your code here
    p opts
    end#class MyApp

    I don't know what #on_tail does either.
    Jim Freeze, Dec 29, 2005
  11. That's a good expectation to have.

    You mention OptionParser has poor documentation. While it's not
    complete, I find the example at [1] is quite adequate.


    Gavin Sinclair, Dec 29, 2005
  12. Please disregard my other reply in this thread. Your points are
    perfectly valid and I'll try to improve the documentation.
    The documentation's not at fault here: knowledge of Ruby is assumed :)
    That will be easy to point out in the docs. Thanks.
    It means to show the "--version" option at the end (the "tail") of the
    options list.
    Hopefully I can clear this up. The difference between parse and parse!
    is that parse! swallows ARGV.
    A "mandatory option" is a contradiction :) You need to test for the
    presence of an option in your own code.
    While not disagreeing in the least, I bet there are plenty of
    badly-documented Perl modules, but they're not the often-used ones, so
    they don't detract from the "Perl modules are well documented" meme.

    Ruby is certainly worse, though!

    Gavin Sinclair, Dec 29, 2005
  13. Gavin Sinclair wrote:

    How's that shell documentation coming along?

    Daniel Berger, Dec 29, 2005
  14. It seems to me that your posting pretty much confirms what I said: I didn't
    claim it's "all you need" but that it gets you pretty far, if you "look
    carefully". :) Admittedly it's not so easy for someone new to Ruby...

    Kind regards

    Robert Klemme, Dec 29, 2005
  15. Not too well, just yet!

    Gavin Sinclair, Dec 29, 2005
  16. A "mandatory option" is a contradiction :) You need to test for the
    This is a fair point, but for complex command lines we sometimes really
    need options that are not optional. For example, I'm working on a ruby
    xmltv-like scraper, that has a bunch of regular options, plus essential
    arguments like --config CONFIG_FILE and --scraper SCRAPER_FILE for
    specifying files that are necessary.

    I want to use options for these rather than arguments because they are
    both files and I prefer that the user specify the option --config or
    --scraper rather than having to get the order of them right on the
    command line.

    So you see, I need an "option" that is mandatory in addition to it
    having a mandatory argument.

    Sure I could do it in my own code - but given this is a common case I'm
    tempted to extend OptionParser to do it so that I can get at the option
    description when I report that its missing you see? This is why it
    should probably be a feature of OptionParser - to tie the error message
    to the switch that's missing. I don't think I can be bothered wading
    into that code myself, so I guess I'll do something a little hackier -
    test my options and repeat the option descriptions.

    But how about it Gavin?

    Also - even if you don't - you should probably clearly document the
    fact this isn't available. The frequent use of "MANDATORY" in the
    example led me and probably many others to think it was bug that my
    mandatory arguments weren't reported as missing. It took me a while to
    realize that the word "argument" was key here. It also took me a while
    to realize that the square brackets were important.
    Rover Rhubarb, May 3, 2007
  17. I agree completely.

    The wording should be: mandatory option argument, not option.

    And yes, it makes sense to /also/ make some options mandatory.
    Felipe Contreras, May 3, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.