# OptionParser - no short options or incomplete options

**URL:** <https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842>\
**Category:** Ruby\
**Created:** [February 22, 2008, 12:34am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842 "2008-02-22T00:34:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 22, 2008, 12:34am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/1 "2008-02-22T00:34:28Z")

</div>

Hello all,

Is it possible to force only complete, long options to be used in  
OptionParser? I’m wanting to force users to enter the entire option for  
certain destructive actions…

I’ve tried only including a ‘–long\_option’ as one of the options, but  
if I use ‘-l’ or even ‘–lon’ as the option it still executes the code  
specified for ‘–long\_option’.

Any and all help is appreciated!!!

–  
Bryan

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 22, 2008, 1:11am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/2 "2008-02-22T01:11:57Z")

</div>

Bryan R. wrote:

> Hello all,
> 
> Is it possible to force only complete, long options to be used in  
> OptionParser? I’m wanting to force users to enter the entire option for  
> certain destructive actions…
> 
> I’ve tried only including a ‘–long\_option’ as one of the options, but  
> if I use ‘-l’ or even ‘–lon’ as the option it still executes the code  
> specified for ‘–long\_option’.
> 
> Any and all help is appreciated!!!
> 
> –  
> Bryan

How about:

require ‘optparse’

val\_I\_will\_use\_for\_destructive\_actions = nil

opts = OptionParser.new do |opts|  
opts.on(’–long\_option’) do |val|  
val\_I\_will\_use\_for\_destructive\_actions = ‘–long\_option’  
end  
end

opts.parse!

if val\_I\_will\_use\_for\_destructive\_actions  
puts ‘destroy stuff’  
end

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 25, 2008, 12:20am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/3 "2008-02-25T00:20:10Z")

</div>

Thanks for responding 7stud. However, I’m not clear on how this will  
solve  
my problem. By typing -l, I’ll still execute this option. I’ve also  
come  
across the case where if I have an option for --migrate VERSION  
(required  
input) and I use the option -m, I’ll get an error because the version  
was  
not included. However, if I use -mig, I get no error and a version  
value of  
nil is passed…

Please help!

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 25, 2008, 1:20am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/4 "2008-02-25T01:20:06Z")

</div>

Bryan R. wrote:

> Thanks for responding 7stud. However, I’m not clear on how this will  
> solve  
> my problem. By typing -l, I’ll still execute this option.

You’ve given no reason why forcing your users to type more than they  
have to is required.

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 25, 2008, 3:07am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/5 "2008-02-25T03:07:14Z")

</div>

I’d just hate for someone to fat-finger a simple option and end up  
deleting  
an entire database worth of data. Am I overlooking something? Also,  
what  
if I wanted -l to do one thing, and --long\_option to do something  
different? I’m not trying to get on anyone’s nerves here, I’m just  
wondering if forcing long options is doable…

–  
Thanks!  
Bryan

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 25, 2008, 4:00am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/6 "2008-02-25T04:00:21Z")

</div>

Bryan R. wrote:

> I’d just hate for someone to fat-finger a simple option and end up  
> deleting  
> an entire database worth of data. Am I overlooking something? Also,  
> what  
> if I wanted -l to do one thing, and --long\_option to do something  
> different? I’m not trying to get on anyone’s nerves here, I’m just  
> wondering if forcing long options is doable…
> 
> –  
> Thanks!  
> Bryan

Personally, I think all the optparse modules in any language are more  
trouble than they are worth. Invariably, you can examine ARGV yourself  
and come up with a solution quicker than trying to figure out how the  
overly complex optparse modules work.

In particular, the ruby optparse module appears to have a design flaw:  
it doesn’t send the option that was actually entered to the on() method.

1. This seems to work:

require ‘optparse’

opts = OptionParser.new do |opts|  
opts.on(’–long\_option’) do |val|  
#destroy stuff  
puts “in first on”  
end

opts.on(’–long\_optio’) do |val|  
#do nothing  
puts “in 2nd on”  
end  
end

opts.parse!

1. Or, you can always do this:

require ‘optparse’

puts ‘start:’  
puts ARGV

prohibited = [’-l’, ‘–l’]

ARGV.delete\_if do |str\_in\_argv|  
prohibited.include?(str\_in\_argv)  
end

puts ‘end:’  
puts ARGV

opts = OptionParser.new do |opts|  
opts.on(’–long\_option’) do |val|  
#destroy stuff  
puts “This won’t output for -l or --l short form.”  
end  
end

opts.parse!

> Also, what  
> if I wanted -l to do one thing, and --long\_option to  
> do something different?

require ‘optparse’

opts = OptionParser.new do |opts|  
opts.on(’–long\_option’) do |val|  
puts ‘in first on’  
end

opts.on(’-l’) do |val|  
puts ‘in 2nd on’  
end  
end

opts.parse!

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [February 25, 2008, 4:24am UTC](https://www.ruby-forum.com/t/optionparser-no-short-options-or-incomplete-options/129842/7 "2008-02-25T04:24:01Z")

</div>

Thanks again 7stud. Your suggestions have answered my ultimate  
question,  
which is “is it possible to have OptionParser only parse on fully  
matching  
options.” The answer obviously seems to be “no.”

I’ll be implementing your 2nd suggestion.

–  
Bryan
