How do you pass the string "*.*" to ruby as a command line parameter?
Asked Answered
B

1

8

code:

  #test_argv.rb
  puts "length: #{ARGV.length} "
  ARGV.each do |a|
    puts "Argument: #{a}"
  end

If I supply the string "*.*" (with or without quotes) when I call the above, I get the following output:

  C:\test>test_argv *.*
  length: 5
  Argument: afile.TXT
  Argument: bfile.TXT
  Argument: cfile.TXT
  Argument: dfile.TXT
  Argument: somethingelse.TXT

i.e., a listing of the files in c:\test.

Other values, like "s*.*" returns somethingelse.TXT, as you'd expect if you were doing file operations -- but I'm not.

But this behaves as would have expected:

  C:\test>test_argv asdf
  length: 1
  Argument: asdf

So my question is, how can I make a user-friendly script that will take "*.*" (etc) as a command line parameter? In addition, where is this documented/explained?

Edit: this happens on windows and linux, 1.8.7 and 1.9.2

Bohrer answered 7/6, 2011 at 16:52 Comment(2)
the solution for windows is to use single quote, e.g., ' * . * '.Bohrer
the solution for Linux, as tadman posted, is to wrap in either double or single quotes.Bohrer
H
6

You may need to put this into literal quotes:

test_argv "*.*"

The quotes should avoid having the command-line arguments get expanded on you prematurely.

Handling answered 7/6, 2011 at 17:2 Comment(5)
+1. The behavior the OP is seeing is globbing being done by the shellParaselene
To expand on this, the problem is your shell is interpreting the wildcard sequence and expanding it to a list of filenames before passing it to Ruby. There's not really anything that your script can do about it, the only way to avoid this behavior is to escape the special characters in the shell (the syntax for doing this may vary from shell to shell).Regular
As I stated in my post, with or without quotes the behavior is the same. Try it. Also, to the SO folks, there's a bug when posting: I posted "If I supply the string " followed by quotes, asterisk, dot, asterisk, quote, but the two asterisks do not appear; they are still there, tho, because I can see them if I edit my post.Bohrer
my bad: wrapped in quotes, it works as desired on Linux, but not in windows, i.e., windows ignores the quotes and does the "globbing"Bohrer
@Bohrer - I've fixed the problem you were having with representing the string in SO. You need to escape it with a slash, i.e. "\*.*"Relucent

© 2022 - 2024 — McMap. All rights reserved.