Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:14:28+00:00 2026-05-13T13:14:28+00:00

In python’s OptionParser , how can I instruct it to ignore undefined options supplied

  • 0

In python’s OptionParser, how can I instruct it to ignore undefined options supplied to method parse_args?

e.g.

I’ve only defined option --foo for my OptionParser instance, but I call parse_args with list: [ '--foo', '--bar' ]

I don’t care if it filters them out of the original list. I just want undefined options ignored.

The reason I’m doing this is because I’m using SCons’ AddOption interface to add custom build options. However, some of those options guide the declaration of the targets. Thus I need to parse them out of sys.argv at different points in the script without having access to all the options. In the end, the top level Scons OptionParser will catch all the undefined options in the command line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:14:28+00:00Added an answer on May 13, 2026 at 1:14 pm

    Per synack’s request in a different answer’s comments, I’m posting my hack of a solution which sanitizes the inputs before passing them to the parent OptionParser:

    import optparse
    import re
    import copy
    import SCons
    
    class NoErrOptionParser(optparse.OptionParser):
        def __init__(self,*args,**kwargs):
            self.valid_args_cre_list = []
            optparse.OptionParser.__init__(self, *args, **kwargs)
    
        def error(self,msg):
            pass
    
        def add_option(self,*args,**kwargs):
            self.valid_args_cre_list.append(re.compile('^'+args[0]+'='))
            optparse.OptionParser.add_option(self, *args, **kwargs)
    
        def parse_args(self,*args,**kwargs):
            # filter out invalid options
            args_to_parse = args[0]
            new_args_to_parse = []
            for a in args_to_parse:
                for cre in self.valid_args_cre_list:
                    if cre.match(a):
                        new_args_to_parse.append(a)
    
    
            # nuke old values and insert the new
            while len(args_to_parse) > 0:
                args_to_parse.pop()
            for a in new_args_to_parse:
                args_to_parse.append(a)
    
            return optparse.OptionParser.parse_args(self,*args,**kwargs)
    
    
    def AddOption_and_get_NoErrOptionParser( *args, **kwargs):
        apply( SCons.Script.AddOption, args, kwargs)
        no_err_optparser = NoErrOptionParser(optparse.SUPPRESS_USAGE)
        apply(no_err_optparser.add_option, args, kwargs)
    
        return no_err_optpars
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python: How to get the caller's method name in the called method? Assume I
Python 2 and 3, are the bytecode (pyo & pyc) backward compatible? can i
Python's arbitrary precision decimals are lovely, but I can't seem to find a way
Python is pretty clean, and I can code neat apps quickly. But I notice
Python has string.startswith() and string.endswith() functions which are pretty useful. What NSString methods can
Python works on multiple platforms and can be used for desktop and web applications,
Python uses the reference count method to handle object life time. So an object
Python's IDLE has 'Check Module' (Alt-X) to check the syntax which can be called
Python's logging functions allow you to pass them multiple arguments that they can interpolate
Python newbie here. I'm writing a script that can dump some output to either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.