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 > Needing with OptionParser
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Needing with OptionParser

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 20h09   #1
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Needing with OptionParser

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

Hello All,

I'm hoping someone can me get around a problem I'm having. I have a
Ruby command-line program where I use OptionParser to parse the program's
options. This program is a wrapper for a bunch of other programs, each of
which also accept command-line options. A typical run command would look
like this:

#> wrapper_app --script-to-run MyScript --script-options "--input
foo.txt--output
bar.txt"

Where the information passed with --script-options is the options to use for
the script to be ran. However, OptionParser in my main wrapper program
attempts to parse the options given by --script-options and throws an error
because they aren't valid options for the main wrapper program. I was
hoping that surrounding them in quotes would cause it to be seen as a single
input string to --script-options but that doesn't seem to be the case. Does
anyone know how I can do what I'm trying to do?

Thanks in advance!!! -- BTR

  Réponse avec citation
Vieux 08/01/2008, 21h34   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Needing with OptionParser

Alle marted=EC 8 gennaio 2008, Bryan Richardson ha scritto:
> Hello All,
>
> I'm hoping someone can me get around a problem I'm having. I have a
> Ruby command-line program where I use OptionParser to parse the program's
> options. This program is a wrapper for a bunch of other programs, each of
> which also accept command-line options. A typical run command would look
> like this:
>
> #> wrapper_app --script-to-run MyScript --script-options "--input
> foo.txt--output
> bar.txt"
>
> Where the information passed with --script-options is the options to use
> for the script to be ran. However, OptionParser in my main wrapper progr=

am
> attempts to parse the options given by --script-options and throws an err=

or
> because they aren't valid options for the main wrapper program. I was
> hoping that surrounding them in quotes would cause it to be seen as a
> single input string to --script-options but that doesn't seem to be the
> case. Does anyone know how I can do what I'm trying to do?
>
> Thanks in advance!!! -- BTR


I had exactly the same problem once. With the of some people on this=20
list, I found the following solution:

require 'optparse'

option_parser =3D OptionParser.new do |o|
o.on('-a', '--a-option', 'something'){}
end

unknown =3D []

begin
option_parser.parse! ARGV
rescue OptionParser::InvalidOption =3D> e
e.recover ARGV
#recover just put the unknown option back into ARGV, so I extract it
#again and put it into unknown
unknown << ARGV.shift

#if ARGV still contains some elements, and the first one doesn't start
#with a -, i.e is the argument for the unknown option, I remove it as=20
#well
unknown << ARGV.shift if ARGV.size>0 and ARGV.first[0..0]!=3D'-'
# go on with parsing
retry
end

p unknown

I hope this s

Stefano

  Réponse avec citation
Vieux 08/01/2008, 21h44   #3
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Needing with OptionParser

Stefano,

Great! Thanks for the . I'll try this out as soon as I can!!!

Bryan

On Jan 8, 2008 2:34 PM, Stefano Crocco <stefano.crocco@alice.it> wrote:

> Alle marted=EC 8 gennaio 2008, Bryan Richardson ha scritto:
> > Hello All,
> >
> > I'm hoping someone can me get around a problem I'm having. I have

> a
> > Ruby command-line program where I use OptionParser to parse the

> program's
> > options. This program is a wrapper for a bunch of other programs, each

> of
> > which also accept command-line options. A typical run command would

> look
> > like this:
> >
> > #> wrapper_app --script-to-run MyScript --script-options "--input
> > foo.txt--output
> > bar.txt"
> >
> > Where the information passed with --script-options is the options to us=

e
> > for the script to be ran. However, OptionParser in my main wrapper

> program
> > attempts to parse the options given by --script-options and throws an

> error
> > because they aren't valid options for the main wrapper program. I was
> > hoping that surrounding them in quotes would cause it to be seen as a
> > single input string to --script-options but that doesn't seem to be the
> > case. Does anyone know how I can do what I'm trying to do?
> >
> > Thanks in advance!!! -- BTR

>
> I had exactly the same problem once. With the of some people on this
> list, I found the following solution:
>
> require 'optparse'
>
> option_parser =3D OptionParser.new do |o|
> o.on('-a', '--a-option', 'something'){}
> end
>
> unknown =3D []
>
> begin
> option_parser.parse! ARGV
> rescue OptionParser::InvalidOption =3D> e
> e.recover ARGV
> #recover just put the unknown option back into ARGV, so I extract it
> #again and put it into unknown
> unknown << ARGV.shift
>
> #if ARGV still contains some elements, and the first one doesn't start
> #with a -, i.e is the argument for the unknown option, I remove it as
> #well
> unknown << ARGV.shift if ARGV.size>0 and ARGV.first[0..0]!=3D'-'
> # go on with parsing
> retry
> end
>
> p unknown
>
> I hope this s
>
> Stefano
>
>


  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 19h11.


É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,12820 seconds with 11 queries