Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 243743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:56:48+00:00 2026-05-11T20:56:48+00:00

EDIT: Please, please , please read the two requirements listed at the bottom of

  • 0

EDIT: Please, please, please 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.

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:

...
$quiet       = ARGV.delete('-d')
$interactive = ARGV.delete('-i')
...
# Deal with ARGV as usual here, maybe using ARGF or whatever.

It’s not quite the normal Unix options syntax, because it will accept options non-option command line parameters, as in “myprog -i foo bar -q“, but I can live with that. (Some people, such as the Subversion developers, prefer this. Sometimes I do too.)

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 “-f filename“?

EDIT:

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:

  1. 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 bin 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.

  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:56:49+00:00Added an answer on May 11, 2026 at 8:56 pm

    Here’s the standard technique I usually use:

    #!/usr/bin/env ruby
    
    def usage(s)
        $stderr.puts(s)
        $stderr.puts("Usage: #{File.basename($0)}: [-l <logfile] [-q] file ...")
        exit(2)
    end
    
    $quiet   = false
    $logfile = nil
    
    loop { case ARGV[0]
        when '-q' then  ARGV.shift; $quiet = true
        when '-l' then  ARGV.shift; $logfile = ARGV.shift
        when /^-/ then  usage("Unknown option: #{ARGV[0].inspect}")
        else break
    end; }
    
    # Program carries on here.
    puts("quiet: #{$quiet} logfile: #{$logfile.inspect} args: #{ARGV.inspect}")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 219k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It turns out that I also need to add 'grails.app'… May 12, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer You don't need Visual Studio to write an Active X… May 12, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer There is no point to that assignment. It's probably just… May 12, 2026 at 11:47 pm

Related Questions

I'm running into a situation where I need the atomic sum of two values
I am developing in python a file class that can read and write a
I have the following set of dates (dd/MM/yyyy) matching events in my database: eventId
I just played with Java file system API, and came down with the following

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.