let optionparse.Optionparser ignore unknown command line switches.

N

News123

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
 
N

News123

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
 
S

Steven W. Orr

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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkxVlAUACgkQRIVy4fC+NyQk8QCfTv3S1/7a8O3XedpjpVzRyWYp
zMkAn3E35Gdw59SC9O/L6GkjN7aheawQ
=hFJx
-----END PGP SIGNATURE-----
 
N

News123

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

Jon Clements

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

News123

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

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...
Well the main application would abort the program. I'ts jsut one module
which would like to pre-fetch some 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?

It's not really what I'm looking for, though it might be what I'll end
up doing.

currently I have several apps all importing one module, but all parsing
different options.

There's one common option and I'd like to have one module behaving
differently depending on this option.

My idea was to just change this one module and leave everything else
unmodified.

Another not so nice solution, which could be a very quick and dirty hack
(to be removed lateron) would be to just add to this one module


if "--myoption" in sys.argv:
dosomething

but I'd have prefered a real Optionparser (ignoring unknown options)
I could even make sure, that the unknown options follow the common options.
 
M

Michele Simionato

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

I will plug in my own work on plac: http://pypi.python.org/pypi/plac
Your problem would be solved as follows:

import plac

@plac.annotations(test=('a flag', 'flag', 't'))
def main(test, *ignore):
print test, ignore

if __name__ == '__main__':
plac.call(main, ["-t","--ignoreme_and_dont_fail"])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top