From: News123 on
I wondered, whether there's a simple/standard way to let
the Optionparser just ignore unknown command line switches.

thanks in advance for any ideas



From: News123 on

On 08/01/2010 01:08 PM, News123 wrote:
> I wondered, whether there's a simple/standard way to let
> the Optionparser just ignore unknown command line switches.
>

In order to illustrate, what I try to achieve:


import optparse
parser = optparse.OptionParser()
parser.add_option("-t","--test",dest="test",action="store_true")
argv=["tst.py","-t","--ignoreme_and_dont_fail"]
try:
(options,args)=parser.parse_args(argv)
except:
# due to --ignoreme_and_dont_fail
# I will end up here and neither options nor
# args will be populated
print "parser error:"
# However I would love to be able to see here
# that options.test is true despite the
# error, that occurred afterwards
print "T",options.test

From: Steven W. Orr on
On 08/01/10 07:27, quoth News123:
> On 08/01/2010 01:08 PM, News123 wrote:
>> I wondered, whether there's a simple/standard way to let
>> the Optionparser just ignore unknown command line switches.
>>
>
> In order to illustrate, what I try to achieve:
>
>
> import optparse
> parser = optparse.OptionParser()
> parser.add_option("-t","--test",dest="test",action="store_true")
> argv=["tst.py","-t","--ignoreme_and_dont_fail"]
> try:
> (options,args)=parser.parse_args(argv)
> except:
> # due to --ignoreme_and_dont_fail
> # I will end up here and neither options nor
> # args will be populated
> print "parser error:"
> # However I would love to be able to see here
> # that options.test is true despite the
> # error, that occurred afterwards
> print "T",options.test
>

You need to let us know *why* you want to do this. My psychotic imagination is
contriving that you want to pass on the ignoremeanddontfail options to
something else. If so, then you should be using -- instead of this. The other
possible scheme to solve your unknown problem is to subclass OptionParser so
it does what you want.

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

From: News123 on
On 08/01/2010 05:34 PM, Steven W. Orr wrote:
> On 08/01/10 07:27, quoth News123:
>> On 08/01/2010 01:08 PM, News123 wrote:
>>> I wondered, whether there's a simple/standard way to let
>>> the Optionparser just ignore unknown command line switches.
>>>
>>
>> In order to illustrate, what I try to achieve:
>>
>>
>> import optparse
>> parser = optparse.OptionParser()
>> parser.add_option("-t","--test",dest="test",action="store_true")
>> argv=["tst.py","-t","--ignoreme_and_dont_fail"]
>> try:
>> (options,args)=parser.parse_args(argv)
>> except:
>> # due to --ignoreme_and_dont_fail
>> # I will end up here and neither options nor
>> # args will be populated
>> print "parser error:"
>> # However I would love to be able to see here
>> # that options.test is true despite the
>> # error, that occurred afterwards
>> print "T",options.test
>>
>
> You need to let us know *why* you want to do this. My psychotic imagination is
> contriving that you want to pass on the ignoremeanddontfail options to
> something else. If so, then you should be using -- instead of this. The other
> possible scheme to solve your unknown problem is to subclass OptionParser so
> it does what you want.

Hi Steven,

'--' is good for many use cases, but not for the one I'm looking at.

in my case one imported module should parse some of the options (but
only the one it understands) already during import.
the main program will have to parse the same options again.



From: Jon Clements on
On 1 Aug, 16:43, News123 <news1...(a)free.fr> wrote:
> On 08/01/2010 05:34 PM, Steven W. Orr wrote:
>
>
>
> > On 08/01/10 07:27, quoth News123:
> >> On 08/01/2010 01:08 PM, News123 wrote:
> >>> I wondered, whether there's a simple/standard way to let
> >>> the Optionparser just ignore unknown command line switches.
>
> >> In order to  illustrate, what I try to achieve:
>
> >> import optparse
> >> parser = optparse.OptionParser()
> >> parser.add_option("-t","--test",dest="test",action="store_true")
> >> argv=["tst.py","-t","--ignoreme_and_dont_fail"]
> >> try:
> >>     (options,args)=parser.parse_args(argv)
> >> except:
> >>     # due to --ignoreme_and_dont_fail
> >>     # I will end up here and neither options nor
> >>     # args will be populated
> >>     print "parser error:"
> >> # However I would love to be able to see here
> >> # that options.test is true despite the
> >> # error, that occurred afterwards
> >> print "T",options.test
>
> > You need to let us know *why* you want to do this. My psychotic imagination is
> > contriving that you want to pass on the ignoremeanddontfail options to
> > something else. If so, then you should be using -- instead of this. The other
> > possible scheme to solve your unknown problem is to subclass OptionParser so
> > it does what you want.
>
> Hi Steven,
>
> '--' is good for many use cases, but not for the one I'm looking at.
>
> in my case one imported module should parse some of the options (but
> only the one it understands) already during import.
> the main program will have to parse the same options again.

Take it up a level.

Dedicate a module (called app_configuration) or something to do the
option parsing -- everything the application can support is listed
there... It's a bad idea to ignore invalid options...

When that's imported it runs the parsing for sys.argv whatever... and
you guarantee a variable called app_settings (or whatever) is present
in that module.

Any module that needs the settings, just imports app_configuration and
tries to take what it understands...

Would that work for you?

Jon.