Question

What I'm trying to do is pass an argument for an option in a ruby script that will be a unix command. The command may (probably will) involve greps, pipes and possible lots of other stuff. Essentially, what I'm wondering is, can a GetOptLong option be setup to accept any character as an argument. For what it's worth, I can't use OptionParser, and probably not slob either (or whatever it's called).

Thanks, -Rob

Was it helpful?

Solution

Pretty sure you can just pass in your unix commands as a string and execute them from within your script.. so something like:

#getoptlong.rb

require 'getoptlong'

opts = GetoptLong.new(
  [ '--unix', GetoptLong::OPTIONAL_ARGUMENT ]
)

opts.each do |opt, arg|
  case opt
    when '--unix'
      puts `#{arg}`
  end
end

and execute the script with something like:

ruby getOptLong.rb --unix "netstat -an | grep '61613'"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top