Difference between revisions of "Skel.rb"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with '=Skelton Ruby Script= <pre> </pre> category:Ruby Category:Examples') |
PeterHarding (talk | contribs) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= | =Skeleton Ruby Script= | ||
See - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/getoptlong/rdoc/GetoptLong.html | |||
<pre> | <pre> | ||
#!/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) | |||
</pre> | </pre> | ||
[[category:Ruby]] | [[category:Ruby]] | ||
[[Category:Examples]] | [[Category:Examples]] |
Latest revision as of 10: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)