Jim Freeze <jim / freeze.org> writes:

> Hi:
> 
> I have a program that takes several parameters from the command line.
> I need to test these parameters that:
>  1) they exist
>  2) they are not nil
>  3) they are not empty
> 
> So, in my code I have
>   if !defined?(param) || param.nil? || param.empty?
>     # err
>   end

How about something like

  err(...) unless ARGV[0] && !ARGV[0].empty?

There's no need to test for defined?, as the array will return nil for
missing elements.

For personal scripts, I tend to use

  file_name = ARGV.shift or raise("Missing file name")
  target    = ARGV.shift or raise("Missing target")

For stuff that's more general, I sometimes define

   def usage
     $stderr.puts "
        <stuff about usage
     "
     exit(1)
   end

  file_name = ARGV.shift or usage
  target    = ARGV.shift or usage
   
It doesn't catch the case when I run it with empty strings, but that's
not something I tend to do too often :)



Dave