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

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
davej
  • 1,350
  • 5
  • 17
  • 34

1 Answers1

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 3
    +1. The behavior the OP is seeing is [globbing](http://en.wikipedia.org/wiki/Glob_%28programming%29) being done by the shell – Abe Voelker Jun 07 '11 at 17:04
  • 1
    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). – bta Jun 07 '11 at 17:05
  • 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. – davej Jun 07 '11 at 17:07
  • 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" – davej Jun 07 '11 at 17:09
  • 1
    @davej - I've fixed the problem you were having with representing the string in SO. You need to escape it with a slash, i.e. "\\*.*" – Dominik Grabiec Jun 07 '11 at 18:16