9
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\""]