views:

60

answers:

4

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

+1  A: 

I think it's unlikely, as far as I know that's all dealt with by the shell before it gets passed to the program.

Gareth
+2  A: 

On Unix systems, the command line shell (Bash, csh, etc.) automatically converts such syntax into argument strings and sends them to the Ruby executable. For instance, * automatically expands to each file in a directory. I doubt there is a way to detect this, and I ask why you want to do so.

mcandre
+2  A: 

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
irkenInvader
I guess this is as close as I can get then
Bub Bradlee
A: 

One wayout could be that you fetch last line of history from shell that u ran the command from... but i don't know how do i do that.

Within the script, I tried...

p `history | tail -1`

and

f = open("|history | tail -1")
foo = f.read()
f.close()
p foo

but it is returning some weired strings :| that s probably because, a new shell is invoked when a ruby script is executed that has no history...

Anybody got clue how to fetch history?

Ninad Pachpute