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

  • SEARCH
  • Home
  • 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 5933339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:57:34+00:00 2026-05-22T14:57:34+00:00

I want either a Ruby method or a regular expression that will let me

  • 0

I want either a Ruby method or a regular expression that will let me split a string of command-line arguments into an ARGV-like array. What I am asking is similar to this question, but in Ruby.

I’m writing unit tests for a Ruby program that processes command-line input using Trollop (though this question would be the same for any other option parser).

The method I want to test looks like this:

def parse_args(args)
  Trollop::options(args) do
    # ... parse options based on flags
  end
end

In my program, I call parse_args(ARGV). In my test, I thought I could just pass in a string split on spaces, but this is not the behavior of ARGV. Compare the following:

./argv_example.rb -f -m "Hello world" --extra-args "-vvv extra verbose"
=> ["-f", "-m", "Hello world", "--extra-args", "-vvv extra verbose"]

'-f -m "Hello world" --extra-args "-vvv extra verbose"'.split
=> ["-f", "-m", "\"Hello", "world\"", "--extra-args", "\"-vvv", "extra", "verbose\""]
  • 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-22T14:57:35+00:00Added an answer on May 22, 2026 at 2:57 pm

    There is Shellwords if you’re using 1.9. If you’re not using 1.9 but do have Rails then you’ll get Shellwords from Rails. In either case, you can use Shellwords.shellwords to parse a string like a POSIX shell does:

    >> Shellwords.shellwords("Where is 'pancakes house'?")
    => ["Where", "is", "pancakes house?"]
    

    If you don’t have Rails or 1.9 then you could probably just grab the shellwords.rb from Rails and use it.

    Update: It looks like Shellwords is available in Ruby 1.8 (thank you Michael Khol). I was getting ambiguous results about which specific versions had it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small Ruby command-line application that uses fileutils from the standard library
I have a command line Ruby app I'm developing and I want to allow
I've got a string that I want to read line-by-line, but I also need
I want to make a window manager in either of these languages (Preferably ruby).
I have a program that I want to either hide or show certain UIbuttons
I want to specify that either fieldname or freetext must always be present in
I want to write a function that accepts a parameter which can be either
Beginning Ruby Question: I'm trying to see if a string variable's contents is either
So all I simply want to do is make a Ruby program that reads
I am implementing the following problem in ruby. Here's the pattern that I want

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.