PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > OptionParser - no short options or incomplete options
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
OptionParser - no short options or incomplete options

Réponse
 
LinkBack Outils de la discussion
Vieux 21/02/2008, 23h34   #1
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut OptionParser - no short options or incomplete options

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 is appreciated!!!

--
Bryan
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 00h11   #2
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: OptionParser - no short options or incomplete options

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

  Réponse avec citation
Vieux 24/02/2008, 23h19   #3
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: OptionParser - no short options or incomplete options

[Note: parts of this message were removed to make it a legal post.]

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 !

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


  Réponse avec citation
Vieux 25/02/2008, 00h20   #4
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: OptionParser - no short options or incomplete options

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

  Réponse avec citation
Vieux 25/02/2008, 02h06   #5
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: OptionParser - no short options or incomplete options

[Note: parts of this message were removed to make it a legal post.]

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


  Réponse avec citation
Vieux 25/02/2008, 03h00   #6
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: OptionParser - no short options or incomplete options

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|
#destroy stuff
puts "in first on"
end

opts.on('--long_optio') do |val|
#do nothing
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|
#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!



--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 25/02/2008, 03h22   #7
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: OptionParser - no short options or incomplete options

[Note: parts of this message were removed to make it a legal post.]

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|
> #destroy stuff
> puts "in first on"
> end
>
> opts.on('--long_optio') do |val|
> #do nothing
> 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|
> #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!
>
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18098 seconds with 15 queries