# OptionParser - no short options or incomplete options

**URL:** <https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685>\
**Category:** ruby-talk\
**Created:** [21 February 2008 23:34 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685 "2008-02-21T23:34:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Richardson1](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bryan\_Richardson1](https://rubytalk.org/u/Bryan_Richardson1)\
**Post date:** [21 February 2008 23:34 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/1 "2008-02-21T23:34:21Z")

</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  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [22 February 2008 00:11 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/2 "2008-02-22T00:11:57Z")

</div>

Bryan Richardson 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|  
&nbsp;&nbsp;opts.on('--long\_option') do |val|  
&nbsp;&nbsp;&nbsp;&nbsp;val\_I\_will\_use\_for\_destructive\_actions = '--long\_option'  
&nbsp;&nbsp;end  
end

opts.parse!

if val\_I\_will\_use\_for\_destructive\_actions  
&nbsp;&nbsp;puts 'destroy stuff'  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [24 February 2008 23:19 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/3 "2008-02-24T23:19:26Z")

</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!

> **···**
>
> On Thu, Feb 21, 2008 at 5:11 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Bryan Richardson 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|  
> > &nbsp;&nbsp;&nbsp;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  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [25 February 2008 00:20 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/4 "2008-02-25T00:20:00Z")

</div>

Bryan Richardson 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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [25 February 2008 02:06 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/5 "2008-02-25T02:06:18Z")

</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
> 
> On Sun, Feb 24, 2008 at 5:20 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Bryan Richardson 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.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [25 February 2008 03:00 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/6 "2008-02-25T03:00:22Z")

</div>

Bryan Richardson 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|  
&nbsp;&nbsp;opts.on('--long\_option') do |val|  
&nbsp;&nbsp;&nbsp;&nbsp;#destroy stuff  
&nbsp;&nbsp;&nbsp;&nbsp;puts "in first on"  
&nbsp;&nbsp;end

&nbsp;&nbsp;opts.on('--long\_optio') do |val|  
&nbsp;&nbsp;&nbsp;&nbsp;#do nothing  
&nbsp;&nbsp;&nbsp;&nbsp;puts "in 2nd on"  
&nbsp;&nbsp;end  
end

opts.parse!

2) Or, you can always do this:

require 'optparse'

puts 'start:'  
puts ARGV

prohibited = ['-l', '--l']

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

puts 'end:'  
puts ARGV

opts = OptionParser.new do |opts|  
&nbsp;&nbsp;opts.on('--long\_option') do |val|  
&nbsp;&nbsp;&nbsp;&nbsp;#destroy stuff  
&nbsp;&nbsp;&nbsp;&nbsp;puts "This won't output for -l or --l short form."  
&nbsp;&nbsp;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|  
&nbsp;&nbsp;opts.on('--long\_option') do |val|  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'in first on'  
&nbsp;&nbsp;end

&nbsp;&nbsp;opts.on('-l') do |val|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in 2nd on'  
&nbsp;&nbsp;end  
end

opts.parse!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [25 February 2008 03:22 UTC](https://rubytalk.org/t/optionparser-no-short-options-or-incomplete-options/44685/7 "2008-02-25T03:22:56Z")

</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
> 
> On Sun, Feb 24, 2008 at 8:00 PM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Bryan Richardson 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|  
> > &nbsp;&nbsp;&nbsp;&nbsp;#destroy stuff  
> > &nbsp;&nbsp;&nbsp;puts "in first on"  
> > end
> > 
> > opts.on('--long\_optio') do |val|  
> > &nbsp;&nbsp;&nbsp;#do nothing  
> > &nbsp;&nbsp;&nbsp;puts "in 2nd on"  
> > end  
> > end
> > 
> > opts.parse!
> > 
> > 2) 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|  
> > &nbsp;&nbsp;&nbsp;&nbsp;#destroy stuff  
> > &nbsp;&nbsp;&nbsp;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|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts 'in first on'  
> > end
> > 
> > opts.on('-l') do |val|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts 'in 2nd on'  
> > end  
> > end
> > 
> > opts.parse!
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
