Stupid optparse question

C

Chris Patti

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 = {}
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.parse!
 
R

Robert Klemme

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:

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
 
R

Ryan Davis

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:

well... they are called "options", not "mandatories" :p

I usually do this near the top:

ARGV << "-h" if ARGV.empty?
 
C

Chris Patti

How can I make my code print the usage even if zero options are given?
=C2=A0I had *thought* opts.on_tail would do this, but it doesn't seem to= be
working. =C2=A0The usage prints properly if I use -h though:

o =3D OptionParser.new do |opts|
=C2=A0 opts.banner =3D "Usage: #$0 [options] [terms]"

=C2=A0 opts.on("-a", "--automatic", "Use current Git repo to determine
current and next release branch and tag names") do |a|
=C2=A0 =C2=A0 options[:automatic] =3D a
=C2=A0 end

=C2=A0 opts.on("-n", "--next-branch [nextbranch]", "The branch you want
this script to create.") do |n|
=C2=A0 =C2=A0 options[:nextbranch] =3D n
=C2=A0 end

=C2=A0 opts.on("-t", "--tag [tag]", "The tag you want the new branch
created from.") do |t|
=C2=A0 =C2=A0 options[:tag] =3D t
=C2=A0 end

=C2=A0 opts.on_tail("-h", "--help", "Show this help message.") do
=C2=A0 =C2=A0 =C2=A0puts opts
=C2=A0 =C2=A0 =C2=A0exit
=C2=A0 end

end

o.parse!

if ARGV.empty?
=C2=A0puts o
else
=C2=A0# do whatever
end

Kind regards

=C2=A0 =C2=A0 =C2=A0 =C2=A0robert

This works perfectly, thanks much!

(I never thought to mix querying ARGV with the OptionsParser class)

-Chris


--=20
Christopher Patti - Geek At Large | GTalk: (e-mail address removed) | AIM:
chrisfeohpatti | P: (260) 54PATTI
"Technology challenges art, art inspires technology." - John Lasseter, Pixa=
r
 
R

Robert Klemme

How can I make my code print the usage even if zero options are given?
=A0I had *thought* opts.on_tail would do this, but it doesn't seem to b= e
working. =A0The usage prints properly if I use -h though:

o =3D OptionParser.new do |opts|
=A0 opts.banner =3D "Usage: #$0 [options] [terms]"

=A0 opts.on("-a", "--automatic", "Use current Git repo to determine
current and next release branch and tag names") do |a|
=A0 =A0 options[:automatic] =3D a
=A0 end

=A0 opts.on("-n", "--next-branch [nextbranch]", "The branch you want
this script to create.") do |n|
=A0 =A0 options[:nextbranch] =3D n
=A0 end

=A0 opts.on("-t", "--tag [tag]", "The tag you want the new branch
created from.") do |t|
=A0 =A0 options[:tag] =3D t
=A0 end

=A0 opts.on_tail("-h", "--help", "Show this help message.") do
=A0 =A0 =A0puts opts
=A0 =A0 =A0exit
=A0 end

end

o.parse!

if ARGV.empty?
=A0puts o
else
=A0# do whatever
end

Kind regards

=A0 =A0 =A0 =A0robert

This works perfectly, thanks much!

(I never thought to mix querying ARGV with the OptionsParser class)

When I think of it you probably also want to consider this variant:

o =3D 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top