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
How to Use Command-Line Arguments. 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.
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).
Press Ctrl twice to invoke the Run Anything popup. Type the ruby script. rb command and press Enter . If necessary, you can specify the required command-line options and script arguments.
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.
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