SuperManEver
SuperManEver

Reputation: 2362

How to get command line argument when script is executable?

I'm working on simple script that setup project structure for me (directories/files). I have an issue with retrieving argument passed as command line argument:

#!/usr/bin/env ruby

project_name = ARGV.first

puts "Provide project name" ; abort if project_name.nil?

When I try to run it, I get:

$ ./creator test
Provide project name

May be I'm not aware of something. When I do without #!/usr/bin/env ruby it works perfect.

Upvotes: 0

Views: 42

Answers (1)

jbr
jbr

Reputation: 6258

Your problem has nothing to do with wether you execute your script with $ ./creator test or $ ruby creator test. The problem is that you always just print a static string with puts "Provide project name". I think this is more what you are after:

#!/usr/bin/env ruby
project_name = ARGV.first
abort if project_name.nil?
puts "Provide #{project_name}"

Here the project_name variable is substituted into the string, by doing #{project_name}.

Upvotes: 1

Related Questions