Difference between revisions of "Skel.rb"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Skelton Ruby Script=
=Skeleton Ruby Script=
 
See - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/getoptlong/rdoc/GetoptLong.html
 


<pre>
<pre>
#!/usr/bin/env ruby
#!/usr/bin/env ruby


require 'getoptlong'
require 'getoptlong'
 
 
# Call using "ruby tsftpc.rb -hftp.ibiblio.org -n21 -uanonymous -ps@s.com"  
# The parameters can be in any order
# The parameters can be in any order 
 
unless ARGV.length >= 0
    puts "Usage:  skel.rb [-v] [-d] -n<port_no> -u<user_name> -p<password>"
    exit
end
 
host_name = port_no = user_name = password = ''
 
# specify the options we accept and initialize the option parser
 
verbose_flg = false
debug_level = 0
 
 
opts = GetoptLong.new(
    [ "--hostname", "-h", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--port", "-n", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--username", "-u", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--pass", "-p", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
    [ "--inc-debug-level", "-d", GetoptLong::NO_ARGUMENT ]
    [ "--set-debug-level", "-D", GetoptLong::REQUIRED_ARGUMENT ]
)
 
# process the parsed options
 
opts.each do |opt, arg|
    case opt
        when '--hostname'
            host_name = arg
        when '--port'
            port_no = arg
        when '--username'
            user_name = arg
        when '--pass'
            password = arg
        when '--inc-debug-level'
            debug_level += 1
        when '--set-debug-level'
            debug_level = int(arg)
        when '--verbose'
            verbose_flg = true
    end
end
 
puts "verbose_flg = #{verbose_flg}"
puts "debug_level = #{debug_level}"
 
if ARGV.length >= 1
    dir = ARGV.shift
    Dir.chdir(dir)
end


unless ARGV.length == 4 
puts Dir.pwd
  puts "Usage: ruby tsftpc.rb -hftp_site_url -nport_no -uuser_name -ppassword" 
  exit 
end 
 
host_name = port_no = user_name = password = '' 


# specify the options we accept and initialize the option parser    
# Dir["config.?"]                    #=> ["config.h"]
# Dir.glob("config.?")                #=> ["config.h"]
# Dir.glob("*.[a-z][a-z]")            #=> ["main.rb"]
# Dir.glob("*.[^r]*")                #=> ["config.h"]
# Dir.glob("*.{rb,h}")                #=> ["main.rb", "config.h"]
# Dir.glob("*")                      #=> ["config.h", "main.rb"]
# Dir.glob("*", File::FNM_DOTMATCH)   #=> [".", "..", "config.h", "main.rb"]


opts = GetoptLong.new(  
rbfiles = File.join("**", "*.rb")
[ "--hostname", "-h", GetoptLong::REQUIRED_ARGUMENT ], 
Dir.glob(rbfiles)
[ "--port", "-n", GetoptLong::REQUIRED_ARGUMENT ], 
[ "--username", "-u", GetoptLong::REQUIRED_ARGUMENT ], 
[ "--pass", "-p", GetoptLong::REQUIRED_ARGUMENT ] 
)  


# process the parsed options 


opts.each do |opt, arg| 
  case opt 
    when '--hostname' 
      host_name = arg 
    when '--port' 
      port_no = arg 
    when '--username' 
      user_name = arg 
    when '--pass' 
      password = arg 
  end 
end
</pre>
</pre>


[[category:Ruby]]
[[category:Ruby]]
[[Category:Examples]]
[[Category:Examples]]

Latest revision as of 09:56, 30 July 2012

Skeleton Ruby Script

See - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/getoptlong/rdoc/GetoptLong.html


#!/usr/bin/env ruby

require 'getoptlong'

# The parameters can be in any order

unless ARGV.length >= 0
    puts "Usage:  skel.rb [-v] [-d] -n<port_no> -u<user_name> -p<password>"
    exit
end

host_name = port_no = user_name = password = ''

# specify the options we accept and initialize the option parser

verbose_flg = false
debug_level = 0


opts = GetoptLong.new(
    [ "--hostname", "-h", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--port", "-n", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--username", "-u", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--pass", "-p", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
    [ "--inc-debug-level", "-d", GetoptLong::NO_ARGUMENT ]
    [ "--set-debug-level", "-D", GetoptLong::REQUIRED_ARGUMENT ]
)

# process the parsed options

opts.each do |opt, arg|
    case opt
        when '--hostname'
            host_name = arg
        when '--port'
            port_no = arg
        when '--username'
            user_name = arg
        when '--pass'
            password = arg
        when '--inc-debug-level'
            debug_level += 1
        when '--set-debug-level'
            debug_level = int(arg)
        when '--verbose'
            verbose_flg = true
    end
end

puts "verbose_flg = #{verbose_flg}"
puts "debug_level = #{debug_level}"

if ARGV.length >= 1
    dir = ARGV.shift
    Dir.chdir(dir)
end

puts Dir.pwd

# Dir["config.?"]                     #=> ["config.h"]
# Dir.glob("config.?")                #=> ["config.h"]
# Dir.glob("*.[a-z][a-z]")            #=> ["main.rb"]
# Dir.glob("*.[^r]*")                 #=> ["config.h"]
# Dir.glob("*.{rb,h}")                #=> ["main.rb", "config.h"]
# Dir.glob("*")                       #=> ["config.h", "main.rb"]
# Dir.glob("*", File::FNM_DOTMATCH)   #=> [".", "..", "config.h", "main.rb"]

rbfiles = File.join("**", "*.rb")
Dir.glob(rbfiles)