dynamically set default value from cfg file through argparse python
<p>I am trying to use <code>argparse</code> module. I am dealing with 2 configuration files:<br>
1.default cfg file 2. File provided by user.   </p>

<p>If no file provided at run time, read from default file. And if user provides a file, use the content of provided file.</p>

<p>My question is : I want to provide <code>default</code> field for each <code>parser.add_agument</code>, so if it's not provided by user,it will always have some default value. These values can be read from either of the file (i.e. default or user given file). I need to check which file is provided and then <code>from default/user-given-file import *</code></p>

<p>My code:<br>
get_args.py</p>

<pre><code>class GetArgs:
   def get_args(self):
       parser = argparse.ArgumentParser(description='foo')
       #
       parser.add_argument(
        '-c', '--config', type=str, help='Provide a file', 
                          required=False, default='settings.cfg')
         #now note that below "default=version" will be from either "settings.cfg" or "user given file"

         # here may be i need to import from file ? 
        parser.add_argument(
        '-v','--version', type=str, help='API version', required=False, default=version)
        args = parser.parse_args()

        # Assign args to variables
        config=args.config
        version = args.version
        retutn config,version
</code></pre>

<p>code to read cfg file: 
read_file.py</p>

<pre><code>import ConfigParser
configParser = ConfigParser.RawConfigParser()
configParser.read(file)
try:
    version= configParser.get('Set', 'version')
    ....
    ....
except ConfigParser.NoOptionError:
    print "Option not found in configuration file!"
    sys.exit(1)
</code></pre>

<p>something like:  </p>

<pre><code>1.python get_args.py                                      #reads "version" from settings.cfg
2.python get_args.py --config /path/to/file/file.cfg      #reads "version" file.cfg   
</code></pre>

<p>It works perfect when i don't provide --config.</p>

<p>Is this possible to read dynamically?</p>