Last active
November 15, 2023 21:37
-
-
Save michaelvdnest/160551860721421f1306bb16204deea9 to your computer and use it in GitHub Desktop.
A sample of Ruby optparse usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'optparse' | |
options = {} | |
class Parser | |
def self.parse(args) | |
options = {} | |
opt_parser = OptionParser.new do |opts| | |
opts.banner = 'Usage: demo.rb [options] ARG...' | |
opts.separator 'A demo of optparse.' | |
opts.separator 'Example: ruby demo.rb -nMICHAEL foo bar' | |
opts.separator '' | |
opts.separator 'Options:' | |
opts.on('-nNAME', '--name=NAME', 'Name to say hello to') do |n| | |
options[:name] = n | |
end | |
opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| | |
options[:verbose] = v | |
end | |
opts.separator '' | |
opts.separator 'Help options:' | |
opts.on('-h', '--help', 'Prints this help') do | |
puts opts | |
exit | |
end | |
end | |
opt_parser.parse!(args) | |
return options | |
end | |
end | |
begin | |
options = Parser.parse ARGV | |
rescue Exception => e | |
puts "Exception encountered: #{e}" | |
exit 1 | |
end | |
#Print the usage if there are no arguments. | |
Parser.parse %w[--help] if ARGV.empty? | |
# Set the verbose option if the ruby verbose flag is on | |
options[:verbose] = true if $VERBOSE | |
# Print options | |
puts options | |
if options[:verbose] | |
@start = Time.now | |
end | |
# process each argument | |
ARGV.each do|a| | |
puts a | |
end | |
if options[:verbose] | |
elapsed = Time.now - @start | |
puts 'Elapsed time: ' + elapsed.to_s + ' seconds' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beautiful! Thank you! ✨ ❤️