Difference between revisions of "Skel.rb"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 7: | Line 7: | ||
#!/usr/bin/env ruby | #!/usr/bin/env ruby | ||
require 'getoptlong' | 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 = '' | |||
# process the parsed options | # 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}" | |||
</pre> | </pre> | ||
[[category:Ruby]] | [[category:Ruby]] | ||
[[Category:Examples]] | [[Category:Examples]] |
Revision as of 10:38, 30 July 2012
Skelton 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}"