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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:11:11+00:00 2026-05-21T18:11:11+00:00

I have a Python application which needs quite a few (~30) configuration parameters. Up

  • 0

I have a Python application which needs quite a few (~30) configuration parameters. Up to now, I used the OptionParser class to define default values in the app itself, with the possibility to change individual parameters at the command line when invoking the application.

Now I would like to use ‘proper’ configuration files, for example from the ConfigParser class. At the same time, users should still be able to change individual parameters at the command line.

I was wondering if there is any way to combine the two steps, e.g. use optparse (or the newer argparse) to handle command line options, but reading the default values from a config file in ConfigParse syntax.

Any ideas how to do this in an easy way? I don’t really fancy manually invoking ConfigParse, and then manually setting all defaults of all the options to the appropriate values…

  • 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-21T18:11:12+00:00Added an answer on May 21, 2026 at 6:11 pm

    I just discovered you can do this with argparse.ArgumentParser.parse_known_args(). Start by using parse_known_args() to parse a configuration file from the commandline, then read it with ConfigParser and set the defaults, and then parse the rest of the options with parse_args(). This will allow you to have a default value, override that with a configuration file and then override that with a commandline option. E.g.:

    Default with no user input:

    $ ./argparse-partial.py
    Option is "default"
    

    Default from configuration file:

    $ cat argparse-partial.config 
    [Defaults]
    option=Hello world!
    $ ./argparse-partial.py -c argparse-partial.config 
    Option is "Hello world!"
    

    Default from configuration file, overridden by commandline:

    $ ./argparse-partial.py -c argparse-partial.config --option override
    Option is "override"
    

    argprase-partial.py follows. It is slightly complicated to handle -h for help properly.

    import argparse
    import ConfigParser
    import sys
    
    def main(argv=None):
        # Do argv default this way, as doing it in the functional
        # declaration sets it at compile time.
        if argv is None:
            argv = sys.argv
    
        # Parse any conf_file specification
        # We make this parser with add_help=False so that
        # it doesn't parse -h and print help.
        conf_parser = argparse.ArgumentParser(
            description=__doc__, # printed with -h/--help
            # Don't mess with format of description
            formatter_class=argparse.RawDescriptionHelpFormatter,
            # Turn off help, so we print all options in response to -h
            add_help=False
            )
        conf_parser.add_argument("-c", "--conf_file",
                            help="Specify config file", metavar="FILE")
        args, remaining_argv = conf_parser.parse_known_args()
    
        defaults = { "option":"default" }
    
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read([args.conf_file])
            defaults.update(dict(config.items("Defaults")))
    
        # Parse rest of arguments
        # Don't suppress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser]
            )
        parser.set_defaults(**defaults)
        parser.add_argument("--option")
        args = parser.parse_args(remaining_argv)
        print "Option is \"{}\"".format(args.option)
        return(0)
    
    if __name__ == "__main__":
        sys.exit(main())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django (Python) project that needs to know what version its code
I developed a command-line utility which needs to be called from a Java GUI
We have a requirement in which we need to change change the words or
I have an application that receives a pointer to JPEG data from a camera
I'm new to WSGI on python; but have a windows server that's got isapi_wsgi
I'm new to Clojure and have been using Compojure to write a basic web
I have an unusual request. I've just moved to a new apartment and I
I wanted to get the community's feedback on a language choice our team is
I like haskell and many things connected with it as its type-engine, lot of
My present task is to dissect tcpdump data that includes P2P messages and I

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.