Search code examples
rubygetopt-long

Can ruby's GetOptLong process spaces in option arguments?


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


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'"