how to create dynamic configuration file using python
Question: I have a python script which is controlled by a config file called
**system.config** .the structure of config file is like bellow with some
default values.

    
    
    [company]
    companyname: XYZ
    
    [profile]
    name: ABC
    joining: 1/1/2014
    

the code for config file is : **config_parser_details.py**

    
    
    import ConfigParser
    import sys
    
    Config = ConfigParser.ConfigParser()
    Config.read("system.config")
    filename = "system.config"
    
    def ConfigSectionMap(section):
      dict1 = {}
      options = Config.options(section)
      for option in options:    
        try:
          dict1[option] = Config.get(section, option)
          if dict1[option] == -1:
             DebugPrint("skip: %s" % option)
        except:
          print("exception on %s!" % option)
          dict1[option] = None
      return dict1
    
    company = ConfigSectionMap("company")['companyname']
    name = ConfigSectionMap("profile")['name']
    joindate = ConfigSectionMap("profile")['joining']
    

now the code for my script is : **test.py**

    
    
    import config_parser_details as p
    import sys
    import warnings
    import os
    
    company = p.company
    
    name = p.name
    date = p.joindate
    
    
    print("%s\n" %company)
    print("%s\n" %name)
    

output is

    
    
    XYZ
    ABC
    

now I want to give input in the config file through command line. like

    
    
    python test.py --compname ="testing"
    

if any argument is missing in the command line than default value will be the
input.


Answer: You could use [argparse](https://docs.python.org/2/library/argparse.html)
library to parse command line arguments.

So your _test.py_ file looks like below :

    
    
    import config_parser_details as p
    import sys
    import warnings
    import os
    import argparse
    
    commandLineArgumentParser = argparse.ArgumentParser()
    commandLineArgumentParser.add_argument("-c", "--compname", help="Company name", default=p.company)
    commandLineArguments = commandLineArgumentParser.parse_args()
    
    company = commandLineArguments.compname
    
    name = p.name
    date = p.joindate
    
    print("%s\n" %company)
    print("%s\n" %name)
    

