How can I make my code print the usage even if zero options are given?
I had *thought* opts.on_tail would do this, but it doesn't seem to be
working. The usage prints properly if I use -h though:
How can I make my code print the usage even if zero options are given?
I had *thought* opts.on_tail would do this, but it doesn't seem to be
working. The usage prints properly if I use -h though:
---
require 'optparse'
options = {}
o = OptionParser.new do |opts|
opts.banner = "Usage: #$0 [options] [terms]"
opts.on("-a", "--automatic", "Use current Git repo to determine
current and next release branch and tag names") do |a|
options[:automatic] = a
end
opts.on("-n", "--next-branch [nextbranch]", "The branch you want
this script to create.") do |n|
options[:nextbranch] = n
end
opts.on("-t", "--tag [tag]", "The tag you want the new branch
created from.") do |t|
options[:tag] = t
end
opts.on_tail("-h", "--help", "Show this help message.") do
puts opts
exit
end
well... they are called "options", not "mandatories"
I usually do this near the top:
ARGV << "-h" if ARGV.empty?
···
On Nov 15, 2010, at 13:27 , Chris Patti wrote:
How can I make my code print the usage even if zero options are given?
I had *thought* opts.on_tail would do this, but it doesn't seem to be
working. The usage prints properly if I use -h though:
(I never thought to mix querying ARGV with the OptionsParser class)
-Chris
···
On Mon, Nov 15, 2010 at 4:45 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On 11/15/2010 10:27 PM, Chris Patti wrote:
How can I make my code print the usage even if zero options are given?
I had *thought* opts.on_tail would do this, but it doesn't seem to be
working. The usage prints properly if I use -h though:
---
require 'optparse'
options = {}
o = OptionParser.new do |opts|
opts.banner = "Usage: #$0 [options] [terms]"
opts.on("-a", "--automatic", "Use current Git repo to determine
current and next release branch and tag names") do |a|
options[:automatic] = a
end
opts.on("-n", "--next-branch [nextbranch]", "The branch you want
this script to create.") do |n|
options[:nextbranch] = n
end
opts.on("-t", "--tag [tag]", "The tag you want the new branch
created from.") do |t|
options[:tag] = t
end
opts.on_tail("-h", "--help", "Show this help message.") do
puts opts
exit
end
end
o.parse!
if ARGV.empty?
puts o
else
# do whatever
end
Kind regards
robert
--
Christopher Patti - Geek At Large | GTalk: cpatti@gmail.com | AIM:
chrisfeohpatti | P: (260) 54PATTI
"Technology challenges art, art inspires technology." - John Lasseter, Pixar
When I think of it you probably also want to consider this variant:
o = OptionParser.new do |opts|
...
end
if ARGV.empty?
puts o
else
o.parse! ARGV
# main
end
Depends on when you want to detect the "emptiness".
Btw, on_tail only determines where the option is printed when printing
usage IIRC.
Kind regards
robert
···
On Mon, Nov 15, 2010 at 11:17 PM, Chris Patti <cpatti@gmail.com> wrote:
On Mon, Nov 15, 2010 at 4:45 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:
On 11/15/2010 10:27 PM, Chris Patti wrote:
How can I make my code print the usage even if zero options are given?
I had *thought* opts.on_tail would do this, but it doesn't seem to be
working. The usage prints properly if I use -h though:
---
require 'optparse'
options = {}
o = OptionParser.new do |opts|
opts.banner = "Usage: #$0 [options] [terms]"
opts.on("-a", "--automatic", "Use current Git repo to determine
current and next release branch and tag names") do |a|
options[:automatic] = a
end
opts.on("-n", "--next-branch [nextbranch]", "The branch you want
this script to create.") do |n|
options[:nextbranch] = n
end
opts.on("-t", "--tag [tag]", "The tag you want the new branch
created from.") do |t|
options[:tag] = t
end
opts.on_tail("-h", "--help", "Show this help message.") do
puts opts
exit
end
end
o.parse!
if ARGV.empty?
puts o
else
# do whatever
end
Kind regards
robert
This works perfectly, thanks much!
(I never thought to mix querying ARGV with the OptionsParser class)