[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String to array as command line

lubomir.markovic

3/26/2006 4:03:00 PM

Hello,

I can't find a function that takes a string as an input, parses it and
returns an array in the exactly same way as command line is parsed into
ARGV array variable.
Could you give me a hint?

Thanks in advance
Lubos

6 Answers

Daniel Harple

3/26/2006 4:14:00 PM

0


On Mar 26, 2006, at 6:03 PM, lubomir.markovic@gmail.com wrote:

> Hello,
>
> I can't find a function that takes a string as an input, parses it and
> returns an array in the exactly same way as command line is parsed
> into
> ARGV array variable.
> Could you give me a hint?
>
> Thanks in advance
> Lubos

require 'shellwords'
args = Shellwords.shellwords('foo\ bar "another shellword"') # =>
["foo bar", "another shellword"]

-- Daniel


Ara.T.Howard

3/26/2006 4:24:00 PM

0

lubomir.markovic

3/26/2006 4:42:00 PM

0

Thank you..
The behavior is not exactly the same (at least on Win) but I think I
can live with it ;-)...

Lubos

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
ARGV.each {|ch|
puts ch
}
puts '********'
args = Shellwords.shellwords('-v "\"\\"')
args.each {|ch|
puts ch
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
>>prog.rb -v "\"\\" "foo\ bar" "another shellword"
-v
"********
c:/ruby/lib/ruby/1.8/shellwords.rb:39:in `shellwords': Unmatched double
quote:
(ArgumentError)
from c:/ruby/lib/ruby/1.8/shellwords.rb:35:in `loop'
from c:/ruby/lib/ruby/1.8/shellwords.rb:35:in `shellwords'

lubomir.markovic

3/26/2006 4:51:00 PM

0

The following error raised:

test.rb:503:in `load': incompatible marshal file format (can't
be read) (TypeError) format version 4.8 required; 114.117 given

I use:

>ruby --version
ruby 1.8.2 (2004-12-25) [i386-mswin32]

Could this be a problem?

L.

lubomir.markovic

3/26/2006 5:39:00 PM

0

I have found the problem:
the correct version is :

def command_line string
Marshal::load(IO::popen("ruby -r yaml -e' print
Marshal::dump(ARGV) ' -- #{ string }"){|io| io.read})
end

the problem was that I put the "-v" parameter in the string and it was
used by ruby itself and thus destroyed the marshaled stream...
the added "--" parametr avoid this behavior.

Thanks, this is exactly what I was looking for..

Ara.T.Howard

3/26/2006 5:56:00 PM

0