I'm trying to access the original command line argument string in Ruby (ie - not using the pre-split/separated ARGV array). Does anyone know how to do this? For example:
$> ruby test.rb command "line" arguments
I want to be able to tell if 'line' had quotes around it:
"command \"line\" arguments"
Any tips? Thanks in advance
In your Ruby programs, you can access any command-line arguments passed by the shell with the ARGV special variable. ARGV is an Array variable which holds, as strings, each argument passed by the shell.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.
In the code you posted, *args simply indicates that the method accepts a variable number of arguments in an array called args . It could have been called anything you want (following the Ruby naming rules, of course).
Each argument on the command line is separated by one or more spaces, and the operating system places each argument directly into its own null-terminated string. The second parameter passed to main() is an array of pointers to the character strings containing each argument (char *argv[]).
As far as I can tell, ruby is not removing those double-quotes from your command line. The shell is using them to interpolate the contents as a string and pass them along to ruby.
You can get everything that ruby receives like this:
cmd_line = "#{$0} #{ARGV.join( ' ' )}"
Why do you need to know what is in quotes? Can you use some other delimiter (like ':' or '#')?
If you need to, you can pass double-quotes to ruby by escaping them:
$> ruby test.rb command "\"line\"" arguments
The above cmd_line
variable would receive the following string in that case:
test.rb comand "line" arguments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With