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 6243635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:10:36+00:00 2026-05-24T12:10:36+00:00

I have a system, where you can modify, which modules will be loaded (and

  • 0

I have a system, where you can modify, which modules will be loaded (and run; “module” is not necessarily python module, it can combine several modules). The program can run module A and B. Now, I want to have an option that every module can define (add) its own parameters. Let’s say A wants to have -n and B wants to have -s for something. But there is one common parameter -c, which the main system itself needs. What is the best way to achieve this?

So far I have been using single optparse.OptionParser instance and passed it to every module, when they are initialized. Then the module can modify (add a new parameter) if needed.

  • 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-24T12:10:37+00:00Added an answer on May 24, 2026 at 12:10 pm

    When I had this problem I ended up using a class derived from ArgumentParser that added the ability to register callback functions that would be executed once the arguments were parsed:

    import argparse
    
    class ArgumentParser(argparse.ArgumentParser):
       def __init__(self, *p, **kw):
          super(ArgumentParser, self).__init__(*p, **kw)
          self._reactions = []
       def add_reaction(self, handler):
          self._reactions.append(handler)
       def parse_known_args(self, args=None, namespace=None):
          (args, argv) = super(ArgumentParser, self).parse_known_args(args, namespace)
          for reaction in self._reactions:
             reaction(args)
          return (args, argv)
    

    This way the parser object still needs to be passed to all the modules to register their command line switches, but the modules can react to the switches “on their own”:

    def arguments_parsed(args):
       if args.datafile:
          load_stuff(args.datafile)
    
    def add_arguments(ap):
       ap.add_argument('--datafile',
             help="Load additional input data")
       ap.add_reaction(arguments_parsed)
    

    This uses argparse, but the same could probably be done with optparse.

    It is not tested with advanced features like subparsers and probably won’t work there, but could easily be extended to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database (which I can not modify) with 6 tables in Oracle
I have a system where employees can upload files. There are three ways Upload
I have a system whereby a user can view categories that they've subscribed to
I have a system where registered users can vote up/vote down comments for a
I have a system that creates an order and that order can be billed
To have a general-purpose documentation system that can extract inline documentation of multiple languages,
Can the calendar control be customsised so that, say you have a system of
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
We have a deployment system at my office where we can automatically deploy a
I have a credit system set up on my site where user A can

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.