I'm using OptionParser
with Ruby.
I other languages such as C, Python, etc., there are similar command-line parameters parsers and they often provide a way to show help message when no parameters are provided or parameters are wrong.
options = {} OptionParser.new do |opts| opts.banner = "Usage: calc.rb [options]" opts.on("-l", "--length L", Integer, "Length") { |l| options[:length] = l } opts.on("-w", "--width W", Integer, "Width") { |w| options[:width] = w } opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end.parse!
Questions:
help
message if no parameters were passed (ruby calc.rb
)?length
is a REQUIRED parameter and user do not pass it or pass something wrong like -l FOO
?optparse lets you supply a default value for each destination, which is assigned before the command line is parsed. Again, the default value for verbose will be True : the last default value supplied for any particular destination is the one that counts.
OptionParser is a class for command-line option analysis. It is much more advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented solution.
Optparse module makes easy to write command-line tools. It allows argument parsing in the python program. optparse make it easy to handle the command-line argument. It comes default with python. It allows dynamic data input to change the output.
Just add the -h key to the ARGV, when it is empty, so you may to do something like this:
require 'optparse' ARGV << '-h' if ARGV.empty? options = {} OptionParser.new do |opts| opts.banner = "Usage: calc.rb [options]" opts.on("-l", "--length L", Integer, "Length") { |l| options[:length] = l } opts.on("-w", "--width W", Integer, "Width") { |w| options[:width] = w } opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end.parse!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With